开发者

simple mysql select and insert not working

I have no idea why this isn't working. I've taken code from a previous project that works fine. I just want to select specific columns. Here's what im trying...

    $result = mysql_query("SELECT  when, where, name FROM tablename ORDER BY count DESC LIMIT 0, 20");

    //I dont really know how to debug php well, so this is all I have to go by to know if its not working.  
    if (!$result) 
    {
         echo "<p>Page load has failed please try again</p>";
    }

if I select all like this it works fine:

$result = mysql_query("SELECT * FROM tablename ORDER BY count DESC LIMIT 0, 20");

If im doing an insert, that doesn't work either...

$query = "INSERT INTO tablename (when, where, name) VALUES ('$when' , '$where' , '$name');";
mysql_query($query);

I'm sure the spelling is correct i've looked at it several times, and even if I put

echo $row["name"]; and the others ect while using the select all * they appear...

It just seems like its happening when im selecting individual columns.

I have other tables, with other sites using the same exact code and its working fine.

How can i fix this? or at least how can i debug the php to get some better error messages?

edit开发者_如何学运维: I'm sure the values going in are good, its just simple helloworld string I've tried including count in the select incase that needed to be there for the sort, but that didnt make it work.


The word where is a reserved word (the WHERE clause of SELECT, UPDATE and DELETE queries). So is when. count is a built-in function name but can be used as an identifier, though you shouldn't.

If you use it as an identifier, you must always enclose it in `backticks` in the query. I recommend not using parts of the query language as column names.

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html


WHERE, WHEN and COUNT are reserved MySQL words (http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html). Use backticks like this.

$result = mysql_query("SELECT `where`, `when`, name FROM tablename ORDER BY `count` DESC LIMIT 0, 20");


where is a reserved sql word, use tablename.where to select the correct column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜