PHP 1-liner each() with mysql_fetch_assoc()

Posted by MVC You Know Me on Stack Overflow See other posts from Stack Overflow or by MVC You Know Me
Published on 2010-05-16T15:13:18Z Indexed on 2010/05/16 15:20 UTC
Read the original article Hit count: 146

Filed under:
|

Hey All.

Trying to create a 1-liner to loop through a mysql result set.

Example:

$sql = "SELECT uid, role FROM usr WHERE uid = '$this->uid'";

$r = db::q($sql);

if($r->rows()) {

$q = mysql_fetch_assoc($r->result);

while(list($k, $v) = each($q)) { // would like to omit line above and consolidate here

$_SESSION['usr'][$k] = $this->$k = $v;

} }

problem is that consolidating while loop like so: while(list($k, $v) = each(mysql_fetch_assoc($r->result))

returns an error a la each() not getting object or array, even though of course it is. I think the problem is a casting issue, but it does not seem you can do: each( (array) mysql_fetch_assoc($r->result))

Any ideas? I like to code as tersely as possible, and having "$q = mysql_fetch_assoc($r->result)" everywhere will annoy me, does already.

Keep posted...

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about php5