-2

我如何得到结果

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

到 PHP 变量

我试过

mysql_num_rows

但它什么也不返回。

4

5 回答 5

0

您应该使用 mysql_result 并获取结果的第一列。像这样:

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

您还可以像这样为列添加别名:

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

如果您要同时选择几列,这可能会更好,并且为了可读性。

于 2013-11-13T07:23:43.300 回答
0

像这样试试。你需要使用 mysql_fetch_assoc 或 mysql_fetch_array 函数

  $result = mysql_query(" SELECT COUNT(user) as total FROM game_paths WHERE user='$user' ");
  $row = mysql_fetch_assoc($result);  
  echo $row['total'];

或者

  $result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
  $row = mysql_fetch_array($result);  
  echo $row[0];

文档链接:http ://us2.php.net/mysql_fetch_array http://www.w3schools.com/php/func_mysql_fetch_array.asp

注意:不推荐使用 mysql_* 函数尝试使用 mysqli 或 PDO

于 2013-11-13T07:25:11.133 回答
0

您可以使用以下代码:

  $result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
  $row = mysql_fetch_array($result);  
  $count= $row[0];

或者

$result = mysql_query("SELECT * FROM game_paths WHERE user='$user'");
$count=mysql_num_rows($result);

这将返回满足条件的行数。

于 2013-11-13T07:25:52.917 回答
0
$result = mysqli_query("SELECT COUNT(user) AS user_count FROM game_paths WHERE user='$user'");
$result_array = mysql_fetch_assoc($result);
$user_count=$result_array['user_count'];

请使用 mysqli_ 而不是 mysql_,因为它在新版本中已弃用

于 2013-11-13T07:27:39.977 回答
0

嘿朋友试试这个代码,我想这会解决你的问题

<?php
 $con=mysql_connect('hostname','DBusername','paassword');
  mysql_select_db('Db_name',$conn);
  $query="SELECT COUNT(user) as total FROM game_paths WHERE user='$user'"; 
   $result=mysql_query($query,$con);
   $row=mysql_fetch_array($result);
   echo $row['total'];
  ?>
于 2013-11-13T07:29:36.910 回答