I want the php to be able to load all the rows in the table into an object each and then add them all to an array and return the array to be converted into java objects etc.
<?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());
?>
asked Feb 17, 2014 at 15:20
2 Answers 2
Why not using the 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
}
answered Feb 17, 2014 at 15:26
Sign up to request clarification or add additional context in comments.
Comments
You can also fetch results into a defined class.
Read this reply: PDO PHP Fetch Class
answered Feb 17, 2014 at 15:54
Comments
default