我希望php能够将表中的所有行加载到每个对象中,然后将它们全部添加到数组中并返回要转换为java对象的数组等。
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$dbhost = "localhost";
$dbname = "Example";
$dbuser = "Example";
$dbpass = "Example";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$objectArray = array();
$stmt = $conn->prepare("SELECT * FROM Table");
while ($row = $stmt->fetch())
{
#add all columns on this row to an object and add to array
}
$stmt->execute();
if(!$stmt)
print_r($dbh->errorInfo());
?>
答案 0 :(得分:1)
为什么不使用PDO::FETCH_OBJ
$array = array();
while ($row = $stmt->fetch(PDO::FETCH_OBJ))
{
#add all columns on this row to an object and add to array
$array[] = $row; // $row is an object now
}
答案 1 :(得分:0)
您还可以将结果提取到已定义的类中。
阅读此回复:PDO PHP Fetch Class