简体   繁体   中英

What is the error in the below Query?

I have written the below query in Drupal6.

$sql = 
    "SELECT registryvalue 
    FROM {muln_registry} 
    WHERE fk_applicationid = %d 
    AND registrykey = '%s'";

$result = db_result(db_query($sql, 32, 'SHOW_SCORE_TO_STUDENT'));

It is supposed to return the value 1. But it is not displaying anything. If i copy and run in mysql editor, it returns correctly.

But now it gives empty. No error in database connections etc. Because other queries are running perfectly.

What could be the error here?

All i can see the error is because of {}

should be

 $sql = "SELECT registryvalue FROM muln_registry WHERE fk_applicationid = %d AND registrykey = '%s'";

Try a

echo '<pre>';
var_dump(db_query($sql, 32, 'SHOW_SCORE_TO_STUDENT'));
echo '</pre>';

And see what data is in the handle. This may give you a better idea of what is going wrong. My suggestion is to switch to PDO but I know that is not going to be a valid option when a system already uses a different DB mechanism.

If 'SHOW_SCORE_TO_STUDENT' is the same every time why not use:

$sql = "SELECT registryvalue FROM {muln_registry} WHERE fk_applicationid = '%d' AND registrykey = 'SHOW_SCORE_TO_STUDENT'";

Also you have ' ' around the second parameter but not the first one, not sure if that might be the problem?

You should try LIKE operator for searching a patter. Eg WHERE column_name LIKE '%searchString' will search where record start with searchString.

For more information on Like you can search for it.

please use:

$sql     = "SELECT registryvalue FROM muln_registry WHERE fk_applicationid = %d AND registrykey = '%s'";

instead of

$sql     = "SELECT registryvalue FROM {muln_registry} WHERE fk_applicationid = %d AND registrykey = '%s'";

{} curly braces have been removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM