use extract() with query result
Posted: Tue Jun 01, 2010 2:48 pm
I'm trying to take the result of my mysqli_query and use extract() to get a set of scalar variables. I've tried things like the following, but can't get anything to work.
$sql = "select field1, field2, field3 from table1 where ID = 'nnnn'";
$res = mysqli_query($mysqli, $sql);
while ($values = mysqli_fetch_assoc($res)) {
extract($values);
//or: $newArray = $values;
extract($newArray);
}
I've also tried creating $newArray inside the while block and doing the extract on $newArray outside the block, but that didn't seem to work either.
I've also made various attempts using 'foreach()'.
All the examples I see do something "simple" like echo or printf. Is what I'm trying to do possible?
I can get results using mysqli_fetch_row(), but this doesn't give an associative array, it seems. And it would be tedious and risky to try to create 10 or more scalar variables from a large select query.
Thanks.
$sql = "select field1, field2, field3 from table1 where ID = 'nnnn'";
$res = mysqli_query($mysqli, $sql);
while ($values = mysqli_fetch_assoc($res)) {
extract($values);
//or: $newArray = $values;
extract($newArray);
}
I've also tried creating $newArray inside the while block and doing the extract on $newArray outside the block, but that didn't seem to work either.
I've also made various attempts using 'foreach()'.
All the examples I see do something "simple" like echo or printf. Is what I'm trying to do possible?
I can get results using mysqli_fetch_row(), but this doesn't give an associative array, it seems. And it would be tedious and risky to try to create 10 or more scalar variables from a large select query.
Thanks.