I want to order the results based on the answer of the sum of mathematics in the main query of the results, here is what I mean:
My current code looks something like this:
$query = "SELECT * FROM foo WHERE foobar='{$fobo}' ORDER BY id DESC"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $percent = round((100 * $row['wins'] / ($row['wins'] + $row['loses']))); echo 'blah blah blah' . $percent; }
As you can see, I'm currently ORDER BY id:
$query = "SELECT * FROM foo WHERE foobar='{$fobo}' ORDER BY id DESC";
but I want to ORDER with the answer $percent , but which is calculated inside the while loop.
I tried to create a query and put it before the main query for results:
$p = "SELECT wins, loses FROM foo WHERE foobar='{$fobo}'"; $pr = mysql_query($p); $pow = mysql_fetch_array($pr); $percent = round((100 * $pow['wins'] / ($pow['wins'] + $pow['loses'])));
but spitting out
Warning: mysql_fetch_array () expects parameter 1 to be a resource, boolean
I think that% calculation can be done in the main request, and not in the php sum, which I think will do it, but I donβt know what the request is.
source share