Hi!,

Need a little help... thanks in advance!!!!

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/exponentialprofi/public_html/affiliates/topreferrals.php on line 22

 (code in question)   
    <?php

    $dsplynumreftop="5";

    $qryreftop = "SELECT usrnam, (usrpoints + usrpointsindirect) AS maxpoints FROM scraffiliateusr ORDER BY maxpoints desc";
    $rslreftop=mysql_query($qryreftop);
    //$arrreftop =  mysql_fetch_row($rslreftop);
    $nreftop = mysql_num_rows($rslreftop);

    for ($i=0; $i<$dsplynumreftop; $i++)
    {
    $arrreftop =  mysql_fetch_row($rslreftop);  <<<<<<<<<<<<<<<<<<< Line 22

    echo "$arrreftop[0]<br>";
    }
    ?>

Thanks,
duchaine

Dani AI

Generated

Short diagnosis: that warning means mysql_fetch_row() was passed something that is not a valid result resource — most commonly mysql_query() returned false (query failed) or no database connection was opened. @Jorge7 and were right to flag the missing connection; was right that you should move off the old `mysql*` API. Below are precise checks and a small, safe example using mysqli.

Immediate troubleshooting checklist:

  • Right after the query, verify the result. If it’s false, print the database error (mysql_error() or $mysqli->error) to see why it failed.
  • Run the same SELECT in the MySQL client/phpMyAdmin to rule out syntax or schema issues (wrong table/column names, permissions).
  • Don’t assume there are N rows: either use LIMIT with your $dsplynumreftop or get the actual row count and use min() so you don’t try to fetch past EOF.
  • Watch for variable overwrites (e.g., $rslreftop reassigned) and ensure the connection uses the correct database.

A minimal, safe mysqli example that uses your display-limit and protects against injection:

$limit = (int)$dsplynumreftop; // cast to int
$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_errno) { die('Connect error: ' . $mysqli->connect_error); }

$sql = "SELECT usrnam, (usrpoints + usrpointsindirect) AS maxpoints
        FROM scraffiliateusr
        ORDER BY maxpoints DESC
        LIMIT ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i', $limit);
$stmt->execute();
$stmt->bind_result($usr, $points);
while ($stmt->fetch()) {
    echo $usr . "<br>";
}
$stmt->close();
$mysqli->close();

If you must keep legacy code briefly, immediately check the query result and dump the error before calling mysql_fetch_row():

if (!$rslreftop) { die('Query failed: ' . mysql_error()); }

Final notes: prefer a while-fetch loop over a fixed for loop, always validate/cast any value inserted into LIMIT, and convert to mysqli or PDO as soon as possible.

Recommended Answers

All 3 Replies

Please read the manual for reference Click Here

It will be remove soon so stop using mysql_* This extension is deprecated

please use PDO or MySQLi

You should use mysqli or pdo. Also you're not opening the connection

To extend Jorge_7's answer - you're not opening the connection to the database. As noted, the mysql api's are deprecated (out of date) so either use pdo or mysqli. Myself I prefer mysqli for MySQL database connections, though I think that PDO is more general and supports other databases as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.