0

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

1

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

0

You can also fetch results into a defined class.

Read this reply: PDO PHP Fetch Class

answered Feb 17, 2014 at 15:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.