Question:

MySQL percent symbol?

by  |  earlier

0 LIKES UnLike

I have these lines in a php script to query the DB.

$rs_menucategories = mysql_query('SELECT `MenuCategoryID`, `MenuCategoryName` FROM `universitymenucategory`');

$q_menuitems = 'SELECT `MenuCategoryID`, `Order`, `ItemName`, `ItemCost`, `ItemDescription`'

. ' FROM `universitymenu`'

. ' WHERE `MenuCategoryID` = %d'

. ' ORDER BY `Order` ASC';

The only reference I can find for using %d is date formatting.

What exactly is %d doing here? Nothing in this query has anything to do with date.

Many thanks

 Tags:

   Report

2 ANSWERS


  1. "where x = "%d" means that the field "x" must end with the character "d"

    "where x = "%d%" means that the field x must contain the character "d" anywhere.

    It is not a reliable query in all mySQL versions.

    The preferred method is " x LIKE '%d'

    (not the "=" sign).

    EDIT: sorry kingmaxi: "$q_menuitems " IS a mysql query!


  2. It's a PHP thing, not a MySQL thing. The '%d' is used later on in the script (I found something similar on the web) in the line:

    $rs_menuitems = mysql_query(sprintf($q_menuitems,

    $crow['MenuCategoryID']));

    The sprintf() function replaces the '%d' with the value in $crow['MenuCategoryID'] at that point to form the full mysql query. The %d simply tells sprintf it's going to be accepting a number as it's replacement value.

    Hope that makes sense!

    EDIT: Just JR, $q_menuitems is not a query at that point it's just a string. You can find a continuation of the script here that shows where the sprintf() function is used to replace the %d with a value contained in a query row:

    http://www.dynamicdrive.com/forums/showt...
You're reading: MySQL percent symbol?

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.