dongyi6269
2012-07-16 02:12 阅读 36
已采纳

在phpmyadmin中查询成功但在php中失败

I would like to ask about the query to select the last record in a table which can match a specific condition.

The following code can successfully execute and gives out the correct record in phpmyadmin

SELECT TYPE FROM log WHERE TechID=4 ORDER By LogTime DESC LIMIT 1

However,when I form the query in my php file and execute, it gives out error.

$query2 = "SELECT Type FROM Log WHERE TechID=".$row1['TechID']."ORDER BY LogTime DESC LIMIT 1"

here is the error message:

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 'BY LogTime DESC LIMIT 1' at line 1.

Can anyone tell me what is happening, myquery should be same as the one I use in the phpmyadmin

Below are the sample records I created to test my file.

LogID TechID ClientID SiteID Type     LogTime
1     2      5        1      Checkin  2012/07/04 09:00
2     4      5        1      Checkin  2012/07/04 09:00
3     2      5        1      Checkout 2012/07/04 10:00
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dqkelut8423 dqkelut8423 2012-07-16 02:19

    I'd put a space before the order statement.

    " ORDER"
    
    点赞 评论 复制链接分享
  • duangangmo0583 duangangmo0583 2016-02-27 02:06

    Another thing that might be an issue (though not here): Strings have to be escaped with double quotes in PHP, not single quotes (please don't ask me why). Thus,

    mysqli_query("select * from tabel where name = 'peter'");
    

    will be false, and

    mysqli_query("select * from tabel where name = \"peter\"");
    

    or

    mysqli_query('select * from tabel where name = "peter"');
    

    will succeed.

    点赞 评论 复制链接分享

相关推荐