Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
fkelly
Former Moderator in Good Standing



Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY

PostPosted: Wed Feb 10, 2010 12:40 pm Reply with quote

Are you ever working with say fetchrow but you are puzzled by how to access the contents? Say you have something like:


Code:
   $sql = 'SELECT COUNT(ride_id), SUM(distance) FROM ' .$prefix.'_rc_rides WHERE 1 ';

   $result = $db->sql_query($sql);
   $row = $db->sql_fetchrow($result);


Now you need to extract what's in the $row. But it can get a little confusing and if so try this code in diagnostic mode:

Code:
   echo '<pre>';

print_r($row);
echo '</pre>';


You'll be able to see exactly how to reference the array elements. I found this somewhere out in Google Land when I was trying to figure out how to get at the results of a COUNT operation.
 
View user's profile Send private message Visit poster's website
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Wed Feb 10, 2010 12:53 pm Reply with quote

I also tend to use var_dump($row); a lot.

_________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. 
View user's profile Send private message
fkelly







PostPosted: Wed Feb 10, 2010 3:25 pm Reply with quote

I like that little tag line of yours Palbin:

Quote:
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it


I've found myself becoming dumber and dumber at writing code the more I learn. Especially using PHPED (Nusphere) that syntax highlights just about any error I can make and writing for a localhost (wampserver) and using our dblog in RN, I pretty much get the shell of an application written then add a few lines at a time incrementally, click on over and see what happens. If I write some SQL statements I'll dump them back to the screen first so see if it makes sense, then I'll try to run them and see if MYSQL burps back at me and then I'll see what's in $row and then I'll do my form to present the data all the while checking for validation errors with my Firefox add on.

Somewhere in the back of my mind there is what they used to call a "conceptual design" for what I'm doing but I try to keep it back there and only call on it occasionally. By the time I could write it out on paper as a detailed design, let alone programming specs I can probably have the application done in incremental steps.


Last edited by fkelly on Wed Feb 10, 2010 4:03 pm; edited 1 time in total 
spasticdonkey
RavenNuke(tm) Development Team



Joined: Dec 02, 2006
Posts: 1693
Location: Texas, USA

PostPosted: Wed Feb 10, 2010 3:45 pm Reply with quote

this made me look back at something I was working on awhile ago..

Code:
<img src="modules/'.$module_name.'/images/IconInfantry.gif" width="36" height="36" border="0" title="L'.$RunningCountLeft.'-R'.$RunningCountRight.'-W'.$biggieSmalls.'-LP '.$LeftWingRadicals.'-RP'.$RightWingExtremists.'-LL'.$LeftyLoosey.'-RR'.$RightyTighty.'" />



I was using image titles so I could mouseover and see what was going on with the script. But your advice is very handy, will use, thanks Smile

lol, like my $variable names? noticed I left this in the script when I tired of working on it.
Image http://www.axis-and-allies.com/all-about-44-infantry.html
(mouseover module icons to see)
 
View user's profile Send private message Visit poster's website
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Fri Feb 12, 2010 7:30 am Reply with quote

fkelly, the issue you raise only occurs when using a function on one of the columns. I simply do the following:

Code:


$sql = 'SELECT COUNT(ride_id) AS cntRide, SUM(distance) AS sumDist FROM ' .$prefix.'_rc_rides WHERE 1 ';
   $result = $db->sql_query($sql);
   $row = $db->sql_fetchrow($result);


Now you can simply reference these by these names (whatever you wanted to call them):

$row['cntRide']
$row['sumDist']

_________________
Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
fkelly







PostPosted: Fri Feb 12, 2010 10:24 am Reply with quote

Yes M. thanks. It looks like fetchrow returns both a numeric index to the array, so you can refer to it as $row[0] and also a field name element. But with COUNT you have to reference it as $row['COUNT(ride_id)] or you can simplify it as in your suggestion and use an AS clause. The little print_r code I originally posted is just helpful if you are like me and get confused about what MYSQL is returning.
 
Palbin







PostPosted: Fri Feb 12, 2010 11:43 am Reply with quote

Remember you can do this to specify what kind of array you want. I have gotten into the habit of always calling it out, usually MYSQL_ASSOC.

$db->sql_fetchrow($result, MYSQL_NUMERIC)
$db->sql_fetchrow($result, MYSQL_ASSOC)
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©