Question:
How do you print a result of a mysql query using PHP that only has one answer ?
hippiejane
2007-05-07 21:20:09 UTC
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 ?
Four answers:
Stinky Pyper
2007-05-07 21:31:58 UTC
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
?
2016-11-01 01:20:13 UTC
Php Mysql Query Result
moo
2007-05-09 03:09:28 UTC
$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!
anonymous
2016-03-19 03:08:44 UTC
if( !empty($nameresult) ){ $query = ("SELECT * FROM $table WHERE name='$nameresult'"); if($result = mysql_query($query)){ while($row = mysql_fetch_array($results)){ //get your data } } if(mysql_num_rows($rowAmount) == 0){ print 'There are no results for this query'; } }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...