tags:

views:

84

answers:

3

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!

+1  A: 

You could make the mysql_fetch_assoc call part of your while condition:

while (($q = mysql_fetch_assoc($r->result)) && list($k, $v) = each($q)) {
    $_SESSION['usr'][$k] = $this->$k = $v;
}
Gumbo
That would try to fetch another record in each iteration while the original code only iterates over the fields of one record.
VolkerK
also a bit more verbose than I'd like.I think I'll try to encapsulate the fetch_assoc call as VolkerK has suggested and see if I can cast it as an array. I did see an OS commerce thread re: this problem and casting was the solution, just cannot cast a method call like mysql_fetch_assoc(), but perhaps (array) $r->assocFetch() will do the trick.
MVC You Know Me
+3  A: 

Do yourself a favor and use PDO:

$query->fetchAll();

MySQLi also has a similar method / function.

Alix Axel
Doesn't look like there's more than one record involved here.
VolkerK
right, just a single row returned.point of the question is, how to pull off a 1-liner
MVC You Know Me
@MVC You Know Me: Same goes for PDO `fetch()` method. I understand your question perfectly and I'm not imposing my answer as the correct one, just though you'd like to know other better options. Feel free to accept any of the already submitted answers and even down-vote this one if you think it's irrelevant. =)
Alix Axel
it's an excellent answer, I'm just stubborn and love my db singleton ;--).
MVC You Know Me
A: 
$_SESSION['usr'] = mysql_fetch_assoc($r->result);

or ( if $_SESSION['usr'] already contains some elements with other keys you want to keep)

$_SESSION['usr'] = array_merge($_SESSION['usr'], mysql_fetch_assoc($r->result));
VolkerK
right, but that will give me $sess['usr'] = array(of $k=>$v), while I want $sess['usr'][$k] = $v for each property.probably a bunch of ways to do this, just curious to see how the gurus pull off 1-liner query results
MVC You Know Me
And what is the difference between those two?
VolkerK
because I want to not only set the user session array, but also set the object props ($this->uid, $this->role, etc.) in 1 go.This works fine, just an extra line:$q = mysql_fetch_assoc($r->result);while(list($k, $v) = each($q)) $_SESSION['HPP'][$k] = $this->$k=$v;then, post-login I set object props with:while(list($k, $v) = each($_SESSION['usr'])) $this->$k=$v;I use setter and getter methods to avoid manually typing out $this->why = "type so much?";$this->because = "I like to waste time";etc.
MVC You Know Me
Volkerk is correct re: how to set the session props in 1 go, so he gets the points (would be nice to be able to get the object props as well, but each() does not play the game I want it to...)
MVC You Know Me
You could implement `__get($name)` and just keep _one_ copy of the values (instead of two, one in _SESSION, one as properties of your object), see http://docs.php.net/language.oop5.magic
VolkerK
MVC You Know Me