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 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据