dongsu3138 2012-06-19 20:56
浏览 40
已采纳

看似简单:检查一个查询中是否从MySQL返回数据

Here is a simple scenario I am contemplating. I have a query that is executed on a MySQL database from PHP. I would like to check if any data was returned from the query. However, in order to perform that check, it pulls out one of the rows of returned data.

Look at this example, and its comments:

$booksGrabber = ("SELECT * FROM table");

if (!$booksGrabber) {
  //Query failed, perhaps a syntax error
  exit;
}

if (!mysql_result($booksGrabber, 0)) {
  //No data was returned :(
  exit;
}

while ($book = mysql_fetch_assoc($booksGrabber)) {
  //mysql_result() stole the first row of returned data
  //So if I was expecting the loop to display 4 results,
  //I only get 3...
}

How can I check if data was returned from the database without running two queries (one to check, the other to display all of the data) or having one of the rows stolen?

  • 写回答

2条回答 默认 最新

  • doufen5175 2012-06-19 20:59
    关注

    Use mysql_num_rows. It was as simple as it looked.

    EDIT: If you want more complicated, add mysql_data_seek($booksGrabber,0).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?