douyue7536 2011-11-26 22:37
浏览 105

如何获取php mysql查询中的行数?

im trying to use this query:

mysql_query("SELECT COUNT(*) FROM Users WHERE (Username=" + $username + "AND Hash=" + $hash + ")")

to return the number of rows for authentication in a php script. but this doesnt return an int? how do I use the return value to get the number of rows?

  • 写回答

4条回答 默认 最新

  • dptiq46022 2011-11-26 22:40
    关注

    You can use mysql_fetch_array to fetch rows from the result:

    $result = mysql_query("SELECT COUNT(*) FROM Users WHERE ...") or die(mysql_error());
    $row = mysql_fetch_array($result);
    echo $row[0];
    

    Or since you only need to fetch a single value you can do this more briefly using mysql_result :

    $result = mysql_query("SELECT COUNT(*) FROM Users WHERE ...") or die(mysql_error());
    echo mysql_result($result, 0);
    

    I'd also suggest that you look at parameterized queries instead of building your SQL queries using string concatenations.

    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊