简体   繁体   中英

php/mysql not counting rows in table

I am trying to run this Query:

$stmt = $conn->prepare("SELECT COUNT(*) as a from session ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$result["a"];

but its just displaying:

-

on its own, with no number of records, i know there is rows because when i run

SELECT COUNT(*) from `session` as a

in PHPMyAdmin it shows all the rows there in the column a

why would this query not work?

Here you go for a single column result you can use fetchColumn() and also you have aliased the table name not the column name

$stmt = $conn->prepare("SELECT COUNT(*)  as a  from `session`");
$stmt->execute(array());
echo $stmt->fetchColumn();

PDOStatement::fetchColumn

try this

$stmt = $conn->prepare("SELECT COUNT(*) as cnt from `session` ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$records["cnt"];

为什么要回显$result ..您尚未声明它。.使用$records

echo '- '.$records["a"];

Try like this

$stmt = $conn->prepare("SELECT COUNT(*) as a from `session` ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$records["a"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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