Re: [PHP] mysql_fetch_row options
On Saturday 26 October 2002 18:23, James Taylor wrote: > There's got to be a better way to go about this: I am constantly doing > mysql queries where I am doing > a count(), so a sample query would be like this: "select count(*) from > database". I'm expecting only > ONE value back exactly, and that's the count results. However, to get this > data into a variable, i'm > having to write code like this: > > $result = mysql_query("select count(*) from database", $db); > $myrow = mysql_fetch_row($result); > $staticvar += $myrow[0]; > > $staticvar will never be an array, it's just a simple variable storing a > number. I *could* do it like this: > > $result = mysql_query("select * from database", $db); > $staticvar += mysql_num_rows($result); > > However, the mysql query will be much, much slower if I do it like this. > > Basically, what I'm asking, is how to do something like: > > $staticvar += mysql_fetch_row($result); > > I want to eliminate step two, and I don't want to involve any temporary > arrays when there's always just one > value. Any suggestions? Thanks a bunch! You can assign the directly into a variable using something like: list($count) = mysql_fetch_array($result); Not sure whether you can do a += into an existing variable though. Why don't you just stuff the above into a function instead? -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* Runt packets */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_fetch_row options
Hi, Saturday, October 26, 2002, 8:23:23 PM, you wrote: JT> There's got to be a better way to go about this: I am constantly doing mysql JT> queries where I am doing JT> a count(), so a sample query would be like this: "select count(*) from JT> database". I'm expecting only JT> ONE value back exactly, and that's the count results. However, to get this JT> data into a variable, i'm JT> having to write code like this: JT> $result = mysql_query("select count(*) from database", $db); JT> $myrow = mysql_fetch_row($result); JT> $staticvar += $myrow[0]; JT> $staticvar will never be an array, it's just a simple variable storing a JT> number. I *could* do it like this: JT> $result = mysql_query("select * from database", $db); JT> $staticvar += mysql_num_rows($result); JT> However, the mysql query will be much, much slower if I do it like this. JT> Basically, what I'm asking, is how to do something like: JT> $staticvar += mysql_fetch_row($result); JT> I want to eliminate step two, and I don't want to involve any temporary JT> arrays when there's always just one JT> value. Any suggestions? Thanks a bunch! (untested :) $result = mysql_query("select count(*) as cnt from database", $db); $staticvar += mysql_result($result,0,0); -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php