本文介绍了PHP PDO提取不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以成功选择一个对象,但是我无法使用以下代码从数据库中获取所有行,有人可以看到任何明显的错误吗?
I can successfully select an object, but I cannot fetch all rows from the database using the following code, can anyone see any obvious errors?
$sql2 = "SELECT ID, Latitude, Longitude, Name FROM Countries";
$stmt2 = $pdo->prepare($sql2);
$stmt2->execute();
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
echo $countryID = $row->ID;
echo $countryName= $row->Name;
echo $longitude2 = $row->Longitude;
echo $latitude2 = $row->Latitude;
}
推荐答案
参数 PDO :: FETCH_ASSOC 告诉PDO将结果作为关联数组返回.所以你可以获取数组不是对象
The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array. SO you can fetch array not object
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
echo $countryID = $row['ID'];
echo $countryName= $row['Name'];
//Rest of the code
}
这篇关于PHP PDO提取不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!