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?

user4803090
  • 49
  • 1
  • 2
  • 8
  • You might find help at [StackOverflow: warning-illegal-string-offset-parameter-in-php-file](http://stackoverflow.com/questions/12191528/warning-illegal-string-offset-parameter-in-php-file) or [StackOverflow: how-to-fix-warning-illegal-string-offset-in-php](http://stackoverflow.com/questions/22279230/how-to-fix-warning-illegal-string-offset-in-php) – Our Man in Bananas May 12 '15 at 20:48
  • Your variable `$row` does not have the fields `'rank'` and `'points'` set - try to use `var_dump` or `print_r` to output the whole thing, in order to see what's wrong (maybe). – Jost May 12 '15 at 20:52
  • Tried with `print_r` and it just prints out numbers from the database. – user4803090 May 12 '15 at 20:56

2 Answers2

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • 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...`. – Don't Panic May 12 '15 at 20:55
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>';
  }
}
Misunderstood
  • 5,534
  • 1
  • 18
  • 25