3
function outputscores($mysqlquery,$reverse=false)
{
    while($row = mysql_fetch_array($mysqlquery))
    {
        echo '<img src="img/date.png" /> ' . date("j M Y",strtotime($row['timestamp'])) . '
        <img src="img/time.png" /> ' . date("H:i:s",strtotime($row['timestamp'])) . '
        <img src="img/star.png" /> '.$row['score'].$_GET["scoretype"].'<br />';
    }
}

I need to reverse the array if $reverse is set to true, but PHP says that the mysql_fetch_array's output is not a valid array. Neither is $mysqlquery itself.

Is there any help?

Wow I've asked so many questions today ._.

EDIT

$result = mysql_query("SELECT * FROM $gid LIMIT 25") or throwerror(mysql_error());

outputscores($result);

EDIT2 Another possible call:

$result = mysql_query("SELECT * FROM ".$gid.",users WHERE users.lastcheck < ".$gid.".timestamp") or throwerror(mysql_error());

outputscores($result);
2
  • 1
    You can ask as many questions as you need, there isn't any limit on those here :). (But there are limits on what can be asked) Commented Mar 30, 2009 at 8:10
  • Just a comment on the code - you REALLY should look into security best practices - NEVER pass unescaped variables directly into SQL queries, or output, you application will be vulnerable to SQL/XSS injections. Commented Mar 30, 2009 at 10:09

3 Answers 3

3

Edit: Change your sql query to this:

$mysqlquery="SELECT * FROM $gid";

Change your function to this:

function outputscores($mysqlquery,$reverse=false)
{
    if ($reverse==true)
        $mysqlquery.=" ORDER BY id DESC LIMIT 25";
    else
        $mysqlQuery.=" LIMIT 25";
    $result=mysql_query($mysqlquery);
    while($row = mysql_fetch_array($result))
    {
       //Do output here
    }
}

Here, by adding the words ORDER BY id DESC you will make the records be ordered in descending order of the 'id' column in your table. By typing ASC instead of DESC you can have them ordered in ascending order. Also, you can replace 'id' with any other column from your table, for example timestamp, score, etc.

Edit: Alternatively, you could also make a different function for adding the LIMIT and ORDER BY clause to the query. Then you could do something like this:

$reverse=false;//or true
$mysqlquery=addOrderBy("SELECT * FROM $gid",$reverse);
outputScores($mysqlquery);
Sign up to request clarification or add additional context in comments.

Comments

3

You are propably trying to reverse the whole resultset, try something like this:

function outputscores($mysqlres,$reverse=false)
{
   $results = array();
   while($row = mysql_fetch_array($mysqlres)){
    print('I found a result: <pre>'.print_r($row, false).'</pre>'); //debugging purposes
    $results[] = $row;
   }
   if($reverse) { $results = array_reverse($results); }
   foreach($results as $row)
   {
    echo '<img src="img/date.png" /> ' . date("j M Y",strtotime($row['timestamp'])) .    '
    <img src="img/time.png" /> ' . date("H:i:s",strtotime($row['timestamp'])) . '
    <img src="img/star.png" /> '.$row['score'].$_GET["scoretype"].'<br />';
  }
}

Oh yes indeed as soulmerge says, you should feed the function a mysql resource and not an actual query. Thus like this:

$query = "SELECT field FROM table";
$res = mysql_query($query);
output_scores($res, true);

In regard to your comment, make sure your query is actually returning results, test it in phpMyAdmin or something or add a simple print in the while loop as I did above.

6 Comments

Warning: Invalid argument supplied for foreach() in snip on line 55
The records should be reversed in the SQL query itself, its not efficient to loop through the records twice and reverse them through PHP.
@click Upvote. It woul indeed be faster to have mysql return them in reverse order. However, the OP specificly asked for the array to be reversed.
IMO he didn't know that the records could be reversed through SQL, but I agree, you could be right
The query does not necessarily return results so that's an issue there :/
|
2

If I get this right, $mysqlquery should be a mysql query result, not an array. There are two cases where mysql_fetch_array() does not return an array:

  1. Either the parameter is not a query result resource (verify with var_dump($mysqlquery)), or
  2. There is simply no result (mysql_fetch_array() returns FALSE).

I hope you're not passing the query itself to mysql_fetch_array() as the name of the variable implies. You must call mysql_query() before you can fetch anything.

The reversing itself can be done by fetching all rows and calling array_reverse(). Example:

$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}
$rows = array_reverse($rows);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.