Php PDO和MySql只输出一行一次
当使用Php PDO和MySql只输出一行一次,php,mysql,pdo,Php,Mysql,Pdo,当使用php的PDO和MySql选择和显示记录时,如何一次打印一行并循环其余行?在下面的代码中,我只需要打印一次$row['parentName'],然后循环遍历所有其他行。什么是有效的方法?我这样做不起作用,我得到了注意:未定义的变量:C:\web\apache\htdocs\index.php中的行,位于第227行 我不介意采取一种全新的方法,因为这还没有实现 $stmt = $conn->prepare($sql); $stmt->execute($thisArray);
php的PDO
和MySql
选择和显示记录时,如何一次打印一行并循环其余行?在下面的代码中,我只需要打印一次$row['parentName']
,然后循环遍历所有其他行。什么是有效的方法?我这样做不起作用,我得到了注意:未定义的变量:C:\web\apache\htdocs\index.php中的行,位于第227行
我不介意采取一种全新的方法,因为这还没有实现
$stmt = $conn->prepare($sql);
$stmt->execute($thisArray);
$rows = $stmt->rowCount();
if($rows >= 1) {
echo '<div>'.$row['parentName'].'</div>'; //Echo only once.
while($row = $stmt->fetch()) {
echo '<div>'.$row['childName'].'</div>';
}
} else {
echo 'We could not find anything to display.';
}
$stmt=$conn->prepare($sql);
$stmt->execute($thisArray);
$rows=$stmt->rowCount();
如果($rows>=1){
回显“”。$row['parentName'].';//仅回显一次。
而($row=$stmt->fetch()){
回显'.$row['childName'].';
}
}否则{
echo“我们找不到任何可以展示的东西”;
}
编辑
我被困在获取部分。试试这个:
$stmt = $conn->prepare($sql);
$stmt->execute($thisArray);
if ($stmt->rowCount() >= 1) {
$array = $stmt->fetchAll();
echo '<div>', $array[0]['parentName'], '</div>';
foreach ($array as $row) {
echo '<div>', $row['childName'], '</div>';
}
} else {
echo 'We could not find anything to display.';
}
$stmt=$conn->prepare($sql);
$stmt->execute($thisArray);
如果($stmt->rowCount()>=1){
$array=$stmt->fetchAll();
回显“”,$array[0]['parentName'],“”;
foreach($array作为$row){
echo“”,$row['childName'],'';
}
}否则{
echo“我们找不到任何可以展示的东西”;
}
您有$rows
->$rows=$stmt->rowCount()代码>但正在尝试使用$row
->$row['parentName']
。您可以移动echo'.$row['parentName'.'代码>在while循环中,使用计数器仅在计数器为0时打印。