I am trying to make a simple query to a small MYSQL table, but when I insert the Where clause, I suddenly get an invalid query error (added a while(mysql_fetch_array ){} when working without the where). The MYSQL console gives a 1064 (syntax) error, however, I checked the MYSQL documentation and I am using the proper syntax as far as I can determine.
<?php
$ind=rand(1,3);
$quote=Null;
$sign=Null;
$afil=Null;
$con=mysql_connect(localhost,root,********);//connect to database
mysql_select_db("phone_site",$con);//select table
$query="SELECT * FROM quotes WHERE index=$ind";//get the row for that index
$data=mysql_query($query);
//print out text
print ("<p id=\"quote\">" . $data['quote'] . "</p>");
print ("<p id=\"ename\">" . $data['sign'] . "</p>");
print ("<p id=\"afill\">-- " . $data['afil'] . "</p>");
mysql_close($con);//close connection
?>
Anyone know what the problem is? I’m using XAMPP. Is there something wrong with its MYSQL?
INDEXis one of MySQL’s Reserved Words :If you have a column with that kind of name, you must quote the column name — see Schema Object Names :
Which means your query should look like this :
To complete my answer, after seeing dustmachine’s one, who said :
I suppose the table has been created with some tool like phpMyAdmin or MySQL Workbench ; and those generaly (always, for some) quote column names, to avoid that kind of problem.
Edit after the comment : I didn’t see, but you are using this kind of code :
The mysql_query function doesn’t diretly return the data : it only returns a “ressource” (quoting the manual’s page) :
Not sure it’ll solve all your problems (nothing being displayed, not even the raw HTML tags is odd), but it might at least help a bit…