-1

Hi i try to extract some data from my DB but i get that error

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Users\malasuerte94\Dropbox\MinecraftSv\minecraftsv\SITE\refferal\top.php on line 117

My code is

<?php
echo '<table class="table table-hover"><tr><th>Player</th><th>Invitati</th></tr>';
$query6 = mysql_query("SELECT `referrer`, count(1) FROM `ref_invites` GROUP BY `referrer` ORDER BY `count(1)` DESC");
while($row6 = mysql_fetch_assoc($query6))
    {
        $taracast = $row6['referrer'];
        $scor = $row6['count(1)'];  

        echo "<tr><td>".$taracast." </td><td> ".$scor."</td></tr>";

    }   
echo "</table>";

?>
samayo
  • 15,152
  • 12
  • 83
  • 101
Malasuerte94
  • 1,414
  • 2
  • 14
  • 18

3 Answers3

2

$query6 failed, returning a boolean instead of a MySQL resource.

To fix it, check if $query6 returns boolean, and if it does, use the MySQL error handling function to figure out what went wrong.

(also, do not use mysql_* anymore, but MySQLi or PDO instead)

Bart Friederichs
  • 32,037
  • 14
  • 96
  • 185
2

I suggest you use, or die (mysql_error()) to handle the errors from your queries, you can start by adding it here, like I have done

mysql_query("SELECT `referrer`, count(1) FROM `ref_invites` GROUP BY `referrer` ORDER BY `count(1)` DESC") or die(mysql_error());

If you add or die (mysql_error()) at the end of your queries, i.e. at the end of your mysql_query() mysql_connect() and mysql_select_db() mysql will always show you, easier-to-understand messages about why it is not working.

samayo
  • 15,152
  • 12
  • 83
  • 101
  • LOL, thanks i found the problem :) i miss DB config. – Malasuerte94 May 20 '13 at 18:22
  • You should emphasize that this should only be used during development as `mysql_error` can reveal sensitive internals. – Gumbo May 20 '13 at 18:23
  • 1
    @Malasuerte94 Yes, as Gumbo said, you should not upload your scripts online, when using `or die (mysql_error())` because, if something goes wrong, it can reveal every structure of your documents, and more information. So, after building your script, replace the part where it says `mysql_error()` to something like `"Database not available"` So, it will echo that text only, instead of it, telling sensitive information about your script – samayo May 20 '13 at 18:26
2

you are missing the mysql_connect(); statement, followed by mysql_select_db();

(Which btw are deprecated. You'd better use mysqli or pdo functions instead.)

By missing these calls, your mysql_query(); returns false (boolean) in stead of a resource.

nl-x
  • 11,501
  • 6
  • 31
  • 58