0

Heres the error:

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home/rspslabe/public_html/Toplist/index.php on line 36

This is the code:

<?

    $maxdate = time() - ($MaxDays * 86400);

    if ($p == 0)
    {
        $site_info = mysql_query("SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY hits DESC, avrating DESC");
    }
    else if ($p == 1)
    {
        $site_info = mysql_query("SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY avrating DESC, hits DESC");
    }

    $site_num = mysql_num_rows($site_info);
    if ($f == "")
        $f = 0;
    $maxsites = $f + $TopListSize;

    if ($site_num > $maxsites)
        $site_num = $maxsites;

    $i = $f;

The actual webpage its self is this: http://rspslab.com/Toplist/

Thanks if anyone could help! :)

  • 1
    `ext/mysql` is outdated and not maintained anymore. It will be flagged as deprecated with PHP5.5. Use `PDO_MYSQL`, or `MySQLi`. http://php.net/en/mysql-connect – KingCrunch Feb 20 '13 at 23:36
  • Where are you setting $p? If it doesn't exist / isn't equal to 0 or 1, neither of those queries will ever be run. Additionally, you shouldn't be embedding parameters in queries like that. Leaves you open to SQL injection - http://php.net/manual/en/security.database.sql-injection.php – Pete Feb 20 '13 at 23:39
  • An example of this may be helpful :P This is how I have coded for the past 2 years. – Laurie Walpole Feb 20 '13 at 23:41
  • Try `mysql_error()` for once. – mario Feb 20 '13 at 23:52
  • I already have :) It came up with a database error, fixed that now it comes up with nothing. – Laurie Walpole Feb 20 '13 at 23:53

1 Answers1

1

Seeing the <?, I'm assuming this is the full code. If this is the full code... You're forgetting to connect to your database.

You can connect to a database by using this code for mysql:

$link = mysql_connect('hostname/ipaddress', 'mysql_user', 'mysql_password');

Please remember however, mysql is outdated and shouldn't be used in new projects, as shown by the red on This site. Instead, I recommend using mysqli (mysql improved).

To use mysqli with your code, you could try this:

$maxdate = time() - ($MaxDays * 86400);
// CONNECT TO THE DATABASE
    $DB_NAME = 'DATABASE_NAME';
    $DB_HOST = 'DATABASE_HOST';
    $DB_USER = 'DATABASE_USER';
    $DB_PASS = 'DATABASE_PASSWORD';

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    if ($p == 0){
    $query = "SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY hits DESC, avrating DESC";

else if ($p == 1){
            $query = "SELECT
    $Members.title AS title, $Members.url AS url, count(*) AS hits,
    avg($IPLogg.vote) AS avrating,
    $Members.siteid AS id FROM 
    $IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
    GROUP BY $Members.siteid ORDER BY avrating DESC, hits DESC";
}
else{ die("P isn't set?");}

    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$maxsites = $f + $TopListSize;
    if($result->num_rows > $maxsites) {
        $site_num = $maxsites;
    }
echo $site_num;

// CLOSE CONNECTION
    mysqli_close($mysqli);

Please note, you must use "mysqli_real_escape_string" on any string inserted into a query. Do this by:

$myescapedstring = $mysqli->real_escape_string($myunescapedstring);

Where $myescapedstring is a string inserted into query, and $myunescapedstring is the string that needs to be escaped.

grepsedawk
  • 3,161
  • 2
  • 25
  • 47