• 0

Need help with my query


Question

I honestly don't know why this is happening.

I'm using this to query my database:

$sql = sprintf("SELECT * FROM options WHERE(key = '%s')", mysql_real_escape_string($option));

It give me this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key = 'test'' at line 1

I've give up, anyone?

Thanks :D

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

If you're using the % wildcard, you need to use the LIKE keyword. So;

$sql = sprintf("SELECT * FROM options WHERE(key LIKE '%s')", mysql_real_escape_string($option));

Should work (:

Link to comment
Share on other sites

  • 0

If you're using the % wildcard, you need to use the LIKE keyword. So;

$sql = sprintf("SELECT * FROM options WHERE(key LIKE '%s')", mysql_real_escape_string($option));

Should work (:

I tired this too:

$sql = "SELECT * FROM options WHERE(key = '".mysql_real_escape_string($option)."')";

Didn't work :(

Link to comment
Share on other sites

  • 0

This has nothing to do with the wildcard or LIKE.

"key" is a reserved keyword in MySQL. Therefore, in order to use it in your query as a field name, you need to surround it with backticks so MySQL treats it as a field name rather than as a keyword.

Also, it's not necessary to put your WHERE condition between parentheses, it's more common to use as few parentheses as possible (although that's just a recommendation).

<?php
$sql = sprintf("SELECT * FROM options WHERE `key` = '%s'", mysql_real_escape_string($option));
?>

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.