Hello,

While using sql queries in drupal modules, %d and %s are often used as a kind of joker, like in this example :

$sql = "INSERT INTO {mysite} (uid, title, created, updated, layout, style, format, status) VALUES (%d, '%s', %d, %d, '%s', '%s', '%s', %d)";
db_query($sql, $form_values['uid'], $form_values['title'], time(), NULL, $form_values['layout'], $form_values['style'], $form_values['format'], $form_values['status']);

But I don't know when to use %d, and when to use %s. Could anyone explain me please ?
Thanks in advance for your help.

Comments

pixel13’s picture

They're just placeholders for the values that follow in the command (e.g. in db_query).
You must use %d for integer values and %s for string values. You can also use %f for a floating point value, %b for binary data and %% just to insert a percent symbol.

kosamo’s picture

Thanks for your quick answer. It's very clear now :)

myatwon’s picture

Hi pixel13,

It can understand well, even it's short..:)
thank you for your contribution...:)

ashlian’s picture

I know this was already answered, but in case anyone else comes across this and needs more info, the following is excerpted from http://us3.php.net/manual/en/function.sprintf.php

A type specifier that says what type the argument data should be treated as. Possible types:
% - a literal percent character. No argument is required.
b - the argument is treated as an integer, and presented as a binary number.
c - the argument is treated as an integer, and presented as the character with that ASCII value.
d - the argument is treated as an integer, and presented as a (signed) decimal number.
e - the argument is treated as scientific notation (e.g. 1.2e+2).
u - the argument is treated as an integer, and presented as an unsigned decimal number.
f - the argument is treated as a float, and presented as a floating-point number (locale aware).
F - the argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3.
o - the argument is treated as an integer, and presented as an octal number.
s - the argument is treated as and presented as a string.
x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).

"Nothing easy is ever simple." -- Plucky Duck

"Nothing easy is ever simple." -- Plucky Duck

pixel13’s picture

Just to be clear: not all of these are implemented in Drupal, particularly if we're referring to db_query function.
In fact PHP query functions don't support this kind of parameter specification, so it is implemented in Drupal. Have a look at database.inc file: you'll find the _db_query_callback function that do the job. And, as you can see, only %d, %b, %s, %% and %f are implemented.

JohnG’s picture

too bad two-character strings like %d and %s are not indexed by the search engine ... druplicate question at http://drupal.org/node/125062 ;)

ikeigenwijs’s picture

I have a ligemit %d argument but also:
AND (DATE_FORMAT(STR_TO_DATE(node_node_data_field_gevolgde_cursus__node_data_field_activiteitsdatum.field_activiteitsdatum_value, '%Y-%m-0T%T'), '%Y-%m-0') >= DATE_ADD(NOW(),INTERVAL -5 YEAR))";

and al the %d get filed in

i tried / / \ \

I found the solution %% is the answer