0

I am having a hard time trying to figure out how to fix the syntax in the code below.

$key = 4;
$column = "five_year";

$query_for_rate = "SELECT $column key FROM $usertable WHERE key=$key";

$get_rate = mysql_query($query_for_rate) or die(mysql_error());

echo $query_for_rate. "<br>";
echo $get_rate;

if($get_rate){
while($row = mysql_fetch_array($get_rate)){

echo $row;

    }
}

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 FROM current_rates WHERE key=4' at line 1


Thank you for the replies. The query is now working, I think. When I echo $get_rate I get "Resource id #5". My problem now is I cannot get my while loop to produce any results. Thank you!

1 Answers1

1

Key is a reserved word. You need to quote it with backticks.

$query_for_rate = "SELECT $column `key` FROM $usertable WHERE `key`=$key";

However I suspect that won't work but rather you'll need to do this instead.

$query_for_rate = "SELECT $column `key` FROM $usertable WHERE `$column`=$key";

Since you need to reference the actual column name not what you aliased it as.

Jonathan
  • 2,778
  • 13
  • 23