Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-09-2013, 10:41 PM   PM User | #1
elem
New Coder

 
Join Date: Sep 2011
Posts: 35
Thanks: 18
Thanked 1 Time in 1 Post
elem is an unknown quantity at this point
Question PDO fetching objects - how to assign variables ?

Hi guys,

very simple I'm sure - what is the better way of assigning variables to rows in this case? I'm assuming this is probably a stupid way of doing it:

PHP Code:
$stmt $conn->prepare('SELECT id,make,model FROM products WHERE id > :id');
$stmt->execute(array('id' => $id));
$result $stmt->fetchAll();
if ( 
count($result) ) {
foreach(
$result as $row) {
$id=$row[0];
$make=$row[1];
$model=$row[2];

elem is offline   Reply With Quote
Old 01-09-2013, 11:58 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep, you can use list.
PHP Code:
list($id$make$model) = $row
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
elem (01-10-2013)
Old 01-10-2013, 09:04 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Tip: you don’t need ->fetchAll() for this, you can loop directly over $stmt
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
elem (01-10-2013)
Old 01-10-2013, 02:06 PM   PM User | #4
elem
New Coder

 
Join Date: Sep 2011
Posts: 35
Thanks: 18
Thanked 1 Time in 1 Post
elem is an unknown quantity at this point
Thanks guys, if there is any more ways I'll be glad to get to know them.

Fou-Lu - I've used it and it works great. Cheers
elem is offline   Reply With Quote
Old 01-10-2013, 02:08 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by elem View Post
if there is any more ways I'll be glad to get to know them.
that depends upon what you want to do with the variables.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:53 AM.


Advertisement
Log in to turn off these ads.