2014-02-17 168 views
0

我希望PHP能夠將表中的所有行加載到每個對象中,然後將它們全部添加到數組中,並返回要轉換爲Java對象的數組等。PHP從行中返回對象數組

<?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()); 

?> 

回答

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 
}