note 116347 added to pdostatement.fetchall
| From: | dyukemedia at gmail dot com | Date: | Tue, 16 Dec 2014 12:16:00 +0000 |
| Subject: | note 116347 added to pdostatement.fetchall | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201205@lists.php.net to get a copy of this message | ||
Getting foreach to play nicely with some data from PDO FetchAll()
I was not understanding to use the $value part of the foreach properly, I hope this helps someone
else.
Example:
<?php
$stmt = $this->db->prepare('SELECT title, FMarticle_id FROM articles WHERE domain_name
=:domain_name');
$stmt->bindValue(':domain_name', $domain);
$stmt->execute();
$article_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
which gives:
array (size=2)
0 =>
array (size=2)
'title' => string 'About Cats Really Long title for the article'
(length=44)
'FMarticle_id' => string '7CAEBB15-6784-3A41-909A-1B6D12667499'
(length=36)
1 =>
array (size=2)
'title' => string 'another cat story' (length=17)
'FMarticle_id' => string '0BB86A06-2A79-3145-8A02-ECF6EA5C405C'
(length=36)
Then use:
<?php
foreach ($article_list as $row => $link) {
echo '<a href="'. $link['FMarticle_id'].'">' .
$link['title']. '</a></br>';
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 122.108.49.36
----
Manual Page -- http://php.net/manual/en/pdostatement.fetchall.php
Edit -- https://master.php.net/note/edit/116347
Del: integrated -- https://master.php.net/note/delete/116347/integrated
Del: useless -- https://master.php.net/note/delete/116347/useless
Del: bad code -- https://master.php.net/note/delete/116347/bad+code
Del: spam -- https://master.php.net/note/delete/116347/spam
Del: non-english -- https://master.php.net/note/delete/116347/non-english
Del: in docs -- https://master.php.net/note/delete/116347/in+docs
Del: other reasons-- https://master.php.net/note/delete/116347
Reject -- https://master.php.net/note/reject/116347
Search -- https://master.php.net/manage/user-notes.php