我正在尝试计算查询返回的行数,我正在这样做:
$what = 'Norman';
$stmt = $conn->prepare('select names as names from names where names = :what');
$stmt->bindParam('what', $what);
$stmt->execute();
$rows = $stmt->fetchColumn();
echo 'Rows found '.$rows;
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($row = $stmt->fetch())
{
echo $row['names'] . "<br>";
}
但我却一无所获。只是空白。这样做的正确方法是什么?
看来您在这里使用了不正确的功能。
$what = 'Norman';
$stmt = $conn->prepare('select names from names where names = ?');
$stmt->execute(array($what));
$rows = $stmt->fetchAll(); // it will actually return all the rows
echo 'Rows found '.count($rows);
foreach ($rows as $row)
{
echo $row['names'] . "<br>";
}
或者你可以通过获取一维数组而不是二维数组来使其更整洁
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
echo 'Rows found '.count($rows);
foreach ($rows as $name)
{
echo $name . "<br>";
}
但你必须先检查 PDO 错误
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)