来自phpmyadmin数据库的php中的count函数无法正常工作[关闭]
Having an issue with getting the code working. It does not seem to be working. I have other codes which work fine which use query string etc... e.g. I used a query string to display the results in the table and that worked fine.
$queryString = "SELECT COUNT(*) FROM `tbl_response`";
$result3 = queryDatabase($queryString);
while($stat = mysql_fetch_array($result3)){
echo $stat
}
在获取代码时遇到问题。 它似乎没有工作。 我有其他使用查询字符串等工作正常的代码...例如 我使用查询字符串在表中显示结果并且运行正常。 p>
$ queryString =“SELECT COUNT(*)FROM`tbl_response`”;
$ result3 = queryDatabase($ queryString);
while($ stat = mysql_fetch_array($ result3)){
echo $ stat
}
code> pre>
div>
Because $stat
is an array not a string, you need to use print_r
or else echo $stat[0]
inside your loop.
Having said that, you're only going to get one row (and one field) returned, so there's an easier way to do this. No loops required:
$queryString = "SELECT COUNT(*) FROM `tbl_response`";
$result3 = queryDatabase($queryString);
echo mysql_result($result3,0);
Side note: you need to migrate to mysqli
or PDO
, you are using deprecated mysql
methods.
Your MySQL syntax seems off. You have it set as:
SELECT COUNT(*) FROM `tbl_response`;
But I would set a name as count
to actually easily access the data like this:
SELECT COUNT(*) as count FROM `tbl_response`;
I’m also unclear as to what your larger PHP framework is to run the query, but the way I would write this out in straight PHP is:
$queryString = mysql_query("SELECT COUNT(*) as count FROM `tbl_response`");
$result3 = mysql_fetch_assoc($queryString);
echo $result3['count'];
And yes, those are using outdated myql_
calls, so this is essentially the same but with mysqli_
instead:
$queryString = mysqli_query("SELECT COUNT(*) as count FROM `tbl_response`");
$result3 = mysqli_fetch_assoc($queryString);
echo $result3['count'];
$queryString = "SELECT COUNT(*) as number_of_responses FROM `tbl_response`";
$result3 = queryDatabase($queryString);
while($stat = mysql_fetch_array($result3)){
echo $stat['number_of_responses'];
}