Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么PDO fetchColumn()在这里不起作用_Php_Pdo - Fatal编程技术网

Php 为什么PDO fetchColumn()在这里不起作用

Php 为什么PDO fetchColumn()在这里不起作用,php,pdo,Php,Pdo,我试图计算查询返回的行数,我是这样做的: $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->

我试图计算查询返回的行数,我是这样做的:

$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 as names from names where name=:what');
$stmt->bindParam('what',$what);
$stmt->execute();
$rows=$stmt->fetchColumn();
回显“已找到行”。$行;
$stmt->setFetchMode(PDO::FETCH_ASSOC);
而($row=$stmt->fetch())
{  
echo$row['names']。“
”; }

但我一直一无所获。一片空白。正确的方法是什么?

如果要获取返回的行数,请使用


看起来您在这里使用了不正确的函数

$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>";
}
$what='Norman';
$stmt=$conn->prepare('从名称=?'的名称中选择名称');
$stmt->execute(数组($what));
$rows=$stmt->fetchAll();//它实际上将返回所有行
回显“找到行”。计数($Rows);
foreach($行作为$行)
{  
echo$row['names']。“
”; }
或者,您可以通过使用一维数组而不是二维数组,使其更加整洁

$rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); 

echo 'Rows found '.count($rows);

foreach ($rows as $name)
{  
    echo $name . "<br>";
}
$rows=$stmt->fetchAll(PDO::FETCH_列,0);
回显“找到行”。计数($Rows);
foreach($行作为$name)
{  
echo$name。“
”; }

但是您必须首先检查PDO错误,是的,这个fetchColumn()在这里看起来很不一致。您是否收到
“找到的行数”的任何输出。$Rows?@YourCommonSense实际上,我刚刚意识到fetchColumn()正在返回一个名称。我原以为它会返回一个数字(select中的行数),我以为这对mysql不起作用,但它实际上是返回表中的行数。我通篇都读到rowCount()不适用于selects{mazed}。@Norman它不适用于某些数据库系统。它没有列出检测到的工作项。phpdelusions.com:)
$rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); 

echo 'Rows found '.count($rows);

foreach ($rows as $name)
{  
    echo $name . "<br>";
}