PDO Return All Rows
You need to use fetchAll()
$result = $query -> fetchAll();
foreach( $result as $row ) {
echo $row['first_name'];
echo $row['last_name'];
}
How do you fetch all rows with pdo?
Your $posts
's value is reset to the latest row when you loop, either you append the value of each post using concat .
operator:
if($count>0){
$posts = "";
foreach($result as $r){
// Define your variable
$posts .= "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
escape($posts);
}
echo"<div id=posts>$posts</div>";
} else { ... }
Or printing each post while looping:
if($count>0){
$posts = "";
echo "<div id='posts'>";
foreach($result as $r){
// Define your variable
$posts = "<div><h2><a href='view_post.php?pid=$id' class='names'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
escape($posts);
echo $posts;
}
echo"</div>";
} else { ... }
PHP - PDO - How to fetch multiple rows with only one query?
You need modify query to
SELECT * FROM users WHERE ID IN(1,4,17);
and use fetchAll() method which returns all records instead of one.
If you don't want to use fetchAll();
then you need use fetch() in loop and you need still modify query.
while ($user = $result->fetch(PDO::FETCH_ASSOC)) {
print_r($user);
}
Notice: you use prepared statements without parameters.
PDO 'fetchAll' return rows with an extra array
fetchAll
will return a two-dimensional array.
- The first dimension is each row.
- The second dimension is each field (key/value pair) within that row.
$resultset[0]['fieldname']
will return the value of fieldname in the first row of the result set.
You're expecting fetchAll
to return a single row whereas that's what fetch
does.
So to keep it simple:
- If your result set will always only return one row, use
fetch
. - If your result set could return multiple (or zero) rows, use
fetchAll
and then for each to iterate through each row.
My PHP PDO fetchAll() code returns one row instead of all results on MySql column table
The fetchAll()
is fine. If you var_dump($result)
you'll see all the rows are there.
This is the problem:
<?php foreach( $result as $row ) ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['category'];?>
Without using curly braces or alternate syntax to group the statements after foreach, only the first statement after the foreach will be repeated. In this case, the first statement after the foreach is nothing. The closing PHP tag implies an instruction separator, so you're effectively saying:
foreach( $result as $row );
i.e. for each row, do nothing. $row
is still defined after the loop as the last row from the array, which is the one option you end up with.
Here's an example of alternate syntax to enclose the two statements:
<?php foreach( $result as $row ): ?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row['category'];?></option>
<?php endforeach; ?>
PHP-PDO: Display all rows
PDO fetch_all
default to fetching both numeric and associative arrays
$tickets = $db->query('SELECT * FROM tickets')->fetchAll();
foreach($tickets as $ticket) {
echo $ticket['name'];
}
Fetch all rows grouped by a specific column using PDO and selecting all columns
Just prepend the asterisk with the table name:
$sql = "SELECT type, my_table.* FROM my_table";
$result = $pdo->query($sql)->fetchAll(\PDO::FETCH_GROUP);
Related Topics
Increase PHP Script Execution Time
How to Get the Last N Items in a PHP Array as Another Array
Tilde Operator in Regular Expressions
How to Prevent from Printscreen of My Webpage
What Does the MySQLi_Error() Expects Parameter 1 to Be MySQLi, Null Given Mean
Undefined Index with PHP Sessions
Which Compression Method to Use in PHP
How to Access Service Container in Symfony2 Global Helper Function (Service)
How to Convert Excel Xls to CSV Using PHP
Getting Output and Exit Status from Shell_Exec()
How to Download Large Files Through PHP Script
How to Send Emails with Arabic Content via PHP's Mail Function
Sort Multidimensional Array by Date Column, Then Use Other Column Values If Dates Are the Same