简体   繁体   English

尝试从php计算mysql db中的行不会返回结果

[英]Attempt to count rows in mysql db from php doesn't return a thing

I'm trying to get the total number of rows in a table using this code: 我正在尝试使用此代码获取表中的总行数:

$count = mysqli_query($con, "SELECT COUNT(*) AS total FROM random_codes");
$count = mysqli_fetch_object($count);
$count = $count->total;

echo "count is $count<br />";

but the echo is always "count is " with no value following. 但是回显始终为“ count is”,没有后续值。 Runing the same SQL code in phpMyAdmin returns the number of rows (above 2000) as total like requested, so the problem is probably in the php code. 在phpMyAdmin中运行相同的SQL代码返回的行数(2000以上)与请求的总数相同,因此问题可能出在php代码中。 Dear responders, I'm new to php & sql so please elaborate and/or link me to documentation. 亲爱的响应者,我是php&sql的新手,请详细说明和/或链接到文档。 Thanks.. 谢谢..

EDIT: attempted error checking, no error shows 编辑:尝试错误检查,没有错误显示

$result = mysqli_query($con, "SELECT COUNT(*) AS total FROM random_codes");
echo mysqli_error($con);
$obj = mysqli_fetch_object($result);
$count = $obj->total;
echo "count is $count<br />";

I was able to duplicate your problem by turning off PHP's error reporting, and making a bad database connection: 我可以通过关闭PHP的错误报告并建立错误的数据库连接来复制您的问题:

error_reporting(0);

$con = new mysqli("localhost", "baduser", "badpw", "SomeDB");

$count = mysqli_query($con, "SELECT COUNT(*) AS total FROM SomeTable");
echo mysqli_error($con);
$count = mysqli_fetch_object($count);
$count = $count->total;

echo "count is $count<br />";

Output: count is 输出:计数为

Try turning on error reporting and double checking your connection information: 尝试打开错误报告并仔细检查您的连接信息:

error_reporting(E_ALL);

Perhaps that may give you an error such as: 也许那会给你一个错误,例如:

Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'baduser'@'localhost' (using password: YES) 警告:mysqli :: mysqli():(28000/1045):用户'baduser'@'localhost'的访问被拒绝(使用密码:是)

If you are on a shared host, often times they default error reporting to a value that may hide certain errors for you, and you'll need to override that while in development mode. 如果您在共享主机上,通常它们默认将错误报告的值设置为可能为您隐藏某些错误的值,并且在开发模式下,您需要覆盖该错误。

I think the problem is in the echo statement.What u need to do is use a . 我认为问题出在echo语句中。您需要做的是使用。 operator between "count is" and the variable $count. “ count is”和变量$ count之间的运算符。

echo "count is". $count."<br />";

If this doesnot work try this. 如果这不起作用,请尝试此操作。

$query=mysql_query("SELECT COUNT(*) AS total FROM random_codes");
$result=mysql_fetch_array($query);
$count=$result['total'];
echo "count is". $count."<br />";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM