duannao1920 2013-11-13 07:20
浏览 17
已采纳

PHP SQL将SELECT COUNT(user)变为变量

How do I get the outcome of

SELECT COUNT(user) FROM game_paths WHERE user = '$user'

to a PHP variable

I tried

mysql_num_rows

but it returns nothing.

  • 写回答

5条回答 默认 最新

  • dpy3846 2013-11-13 07:23
    关注

    You should use mysql_result and get the first column of the result. Like this:

    $result = mysql_query("SELECT COUNT(user) FROM game_paths WHERE user='$user'");
    $count = mysql_result($result, 0);
    

    You can also alias the column like this:

    $result = mysql_query("SELECT COUNT(user) AS total FROM game_paths WHERE user='$user'");
    $data = mysql_fetch_assoc($result);
    $count = $data['total'];
    

    Which might be better if you're going to select several columns at the same time, and also for readability.

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失