我有一个用PDO编写的脚本,它获取用户拥有的消息量并显示它们。我的问题是它没有显示金额。
这是我的代码:
<?php
$con = new PDO("mysql:host=localhost;dbname=dbname", dbuser, dbpass);
$username = $_GET['username'];
$messages = $stmt = $con->prepare("SELECT NULL FROM messages WHERE username=:username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$row = $stmt->fetch();
echo $messages;
?>
答案 0 :(得分:-2)
如果要通过select语句计算行返回次数,请使用rowCount()函数 -
<?php
$con = new PDO("mysql:host=localhost;dbname=dbname", dbuser, dbpass);
$username = $_GET['username'];
$messages = $stmt = $con->prepare("SELECT field1,field2 FROM messages WHERE username=:username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$total = $stmt->rowCount();
echo $total;
?>