Friday, May 21, 2010

How do you print a result of a mysql query using PHP that only has one answer ?

I know this might sound like a really simple or really stupid question but how do you print a result of a mysql query using PHP when the query is a SUM of columns and the result would only be one answer. For some reason I can only find how to print the result in an array and thats obviously when you got more than one answer. I'm probbaly being really thick and the answer is staring me right in the face. Please help ?

How do you print a result of a mysql query using PHP that only has one answer ?
It will come back as a standard result set with only one row and one column which will contain the SUM. Just use the existing mysql php functions for working with results to access it.





$query = "Blah Blah SUM Blah Blah";


$result = mysql_query($query);


$row = mysql_fetch_array($row);


print_r($row); // This will show it to you
Reply:$result = mysql_query("blah SUM blah");


if (!$result) {


echo "Could not connect: ' . mysql_error()";


exit;


}


$row = mysql_fetch_row($result);


$yourVariable = $row['0'];


echo "$yourVarible";





One quick note... The results are loaded into an array with the variable assigned as $row. Therefore if you have multiple results you can get them too using $row['1'] or $row['2'] etc.





Good Luck!

greeting cards

No comments:

Post a Comment