我想将count
的值放入变量并在页面上显示。
这是我的代码:
<?php
try {
$pdo = new PDO('Database info');
} catch (PDOException $e) {
exit('Database error.');
}
$query = $pdo->prepare("select count from counter where count_id=1");
$query->execute();
return $query;
echo $query;
?>
这不起作用...有关我应该如何更改此计数以使计数显示在变量中的任何建议?
感谢。
答案 0 :(得分:1)
更改为:
$query = $pdo->prepare("select count(*) from counter where count_id=1");
$query->execute();
$count = $query->fetchColumn();
echo $count;
答案 1 :(得分:0)
按如下方式修改查询
$count = $con->query("SELECT COUNT(*) as num from counter where count_id=1")
->fetch(PDO::FETCH_ASSOC);
echo $count['num'];