Adam Adam - 7 months ago 45
PHP Question

php pdo - Split fetch() return into Variables

I have the following PHP PDO code:

$sql='SELECT a,b,c FROM my_table WHERE d=?;';
$pds=$database->pdo->prepare($sql); $pds->execute(array($d_value)); $row=$pds->fetch();
echo $row['a'];


this works fine.

I wanted to know how I could break all 3 return values in the fetch() into variables in one line. I have tried this unsuccessfully:

$sql='SELECT a,b,c FROM my_table WHERE d=?;';
$pds=$database->pdo->prepare($sql); $pds->execute(array($d_value)); $row=$pds->fetch();
list($a,$b,$c) = $row[0];
echo $a


how do I get the 'list($a,$b,$c) = $row[0];' line to work?

thankyou very much :)

Answer

Does this work:

list($a,$b,$c) = $row;

With $stmt->fetch() only return one row. no need to reference $row[0]

Comments