在php中只获得1行数

时间:2015-03-20 06:22:08

标签: php

此代码中有错误吗?或者我的错误在其他地方。

$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT count(*) FROM user";
$result = $conn->query($sql_count);
$total_row = $result_count->num_rows;

3 个答案:

答案 0 :(得分:1)

是计数会聚合并只给你1个值,你需要这样做

$sql_count = "SELECT count(*) as total FROM user";
$result = $conn->query($sql_count);
$arrayresult = $result->fetch_array();
echo $arrayresult['total]

答案 1 :(得分:0)

如果您需要数据和结果计数,则必须使用此

$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT * FROM user";
$result = $conn->query($sql_count);
$total_row = $result->num_rows;

如果你只是需要计数rahul解决方案是好的和最佳的

$sql_count = "SELECT count(*) as total FROM user";
$result = $conn->query($sql_count);
$arrayresult = $result->fetch_array();
echo $arrayresult['total]

答案 2 :(得分:-2)

$conn = mysqli_connect("localhost","root","root","sajan")
or die("Unable to connect to MySQL");   
$sql_count = "SELECT count(*) FROM user";
$result = $conn->query($sql_count);
$total_row = $result->num_rows;

会做的伎俩,你引用$ result_count;没有设定。

问候。