CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   PDO fetching objects - how to assign variables ? (http://www.codingforums.com/showthread.php?t=285540)

elem 01-09-2013 10:41 PM

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];



Fou-Lu 01-09-2013 11:58 PM

Yep, you can use list.
PHP Code:

list($id$make$model) = $row


Dormilich 01-10-2013 09:04 AM

Tip: you don’t need ->fetchAll() for this, you can loop directly over $stmt

elem 01-10-2013 02:06 PM

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

Dormilich 01-10-2013 02:08 PM

Quote:

Originally Posted by elem (Post 1305277)
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.


All times are GMT +1. The time now is 02:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.