0

I got the following code:

<?php

require('connect.php');

if(isset($_GET['region'])) {
    $region = mysql_real_escape_string($_GET['region']);
    if ($region == "eu") {
        $rows = mysql_fetch_array(mysql_query("SELECT  * FROM `daily_leaderboard_eu`"));
    } elseif ($region == "na") {
        $rows = mysql_fetch_array(mysql_query("SELECT  * FROM `daily_leaderboard_na`"));
    }
} else {
    $rows = mysql_fetch_array(mysql_query("SELECT  * FROM `daily_leaderboard_eu`"));
}

?>

<table>
    <tr>
        <td>Rank</td>
        <td>Points</td>
    </tr>
    <tr>
        <?
            if(empty($rows)){
                echo '<td colspan="5">We are unable to provide data at the moment!</td>';
            } else {
                foreach($rows as $row){?>
                    <td><?=$row['rank']?></td>
                    <td><?=$row['points']?></td>
                <?}
            }
        ?>
    </tr>
</table>

The code is supposed to fetch the whole daily_leaderboard_X table and then present the data in a simple table. It's however not working as I get the following errors:

Warning: Illegal string offset 'rank' in /home/bbb/public_html/pvp/index.php on line 29 5
Warning: Illegal string offset 'points' in /home/bbb/public_html/pvp/index.php on line 30 5

Picture: http://i.gyazo.com/b1ec034e7d0d906fb81c8175300f288c.png

Could someone point out my mistake and how to fix it please?

3

2 Answers 2

1

When you do this

$rows = mysql_fetch_array(mysql_query("SELECT  * FROM `daily_leaderboard_eu`"));

you are selecting ONE row from the query result. Then when you do this

foreach($rows as $row){?>

you are iterating over the values in the row. So when you do this

$row['rank']

$row is not an array, it is a string, so PHP is expecting a numeric index. That is why you are seeing the "Warning: Illegal string offset". You can fix this by separating your query from your fetch, so at the top you will have just

$rows = mysql_query("SELECT  * FROM `daily_leaderboard_eu`");

Then you can change your foreach loop to a while loop and fetch the rows one at a time.

while ($row = mysql_fetch_array($rows)) {?>

Then $row will actually be an array and work as you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

I used the mysql functions that you had in your question, but you should really update your code to stop using these outdated functions. In this case I think it would just be as simple as changing it to say mysqli... everywhere it says mysql....
0

It is saying there is a typo here:

Where "points" and "rank" are not correct values.


This is how you stay out of this kind of trouble:

if(isset($_GET['region'])) {
  $region = mysql_real_escape_string($_GET['region']);
  if ($region == "eu") {
      $sql = "SELECT   `rank`,`points`  FROM `daily_leaderboard_eu`");
  } 
  elseif ($region == "na") {
      $sql = mysql_query("SELECT   `rank`,`points`  FROM `daily_leaderboard_na`");
  }
  else {
    $sql = "SELECT  `rank`,`points` FROM `daily_leaderboard_eu`");
  } 
  $results = mysql_query($sql);  
  $rows = mysql_num_rows($results);
  if($rows == 1)){
    echo '<p>We are unable to provide data at the moment!</p>';
  }
  else {
    echo '<table><tr><td>Rank</td><td>Points</td></tr><tr>';
    while($row = mysql_fetch_array($results,MYSQL_NUM));
       echo "<td>$row[0]</td><td>$row[1]</td>";
    }
    echo '</tr></table>';
  }
}

3 Comments

Hmm, what do you mean by not correct values? Thos are the column names in the db i.gyazo.com/9bebb30d43db84792ce48981fc81de26.png
Ok done finally. It is worth a look to see how to stay out of trouble like that.
Ok now,finally, I'm really done. It is worth a look to see how to stay out of trouble like that.

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.