Warning: mysql_num_rows(): supplied argument is not a valid

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Warning: mysql_num_rows(): supplied argument is not a valid

Post by a94060 »

Hi,this may seem like a stupid error but for some reason i cannot seem to find the problem to it.
Error is :Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/noob/public_html/index.php on line 51

The part which this corralates to is

Code: Select all

$sql = "SELECT * FROM updates";
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
		  if($rows == 0) {
		  echo 'NO Updates in the Database right now';
		  }
		  else {
		  		while($result = mysql_fetch_array($result)) {
		  		echo 'Date:' .$result['date'];
		  		echo 'Time:' .$result['time'];
		  		}
		}
im pretty sire this is a simple fis,i just cant seem to find the error.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

You might want to use mysql_affected_rows() instead.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Warning: mysql_num_rows(): supplied argument is not a va

Post by Christopher »

Remember to always check for errors:

Code: Select all

$sql = "SELECT * FROM updates";
$result = mysql_query($sql);
if ($errmsg = mysql_error($result)) {
    echo "Error: $errmsg";
} else {
        $rows = mysql_num_rows($result);
		  if($rows == 0) {
		      echo 'NO Updates in the Database right now';
		  } else {
		  	while($result = mysql_fetch_array($result)) {
		  		echo 'Date:' .$result['date'];
		  		echo 'Time:' .$result['time'];
		  	}
		}
}
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dadgummit arborint, you beat me to it :oops: ...

mysql_affect_rows is for INSERT/UPDATE/DELETE queries. Try this and see what pops out...

Code: Select all

<?php
$sql = "SELECT * FROM updates";
$result = mysql_query($sql) or die("Could not run the query: " . mysql_error());
$rows = mysql_num_rows($result);
if ($rows == 0) {
    echo 'NO Updates in the Database right now';
}
else {
    while($result = mysql_fetch_array($result)) {
        echo 'Date:' .$result['date'];
        echo 'Time:' .$result['time'];
    }
}
?>
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

Everah wrote:Dadgummit arborint, you beat me to it :oops: ...

mysql_affect_rows is for INSERT/UPDATE/DELETE queries. Try this and see what pops out...

Code: Select all

<?php
$sql = "SELECT * FROM updates";
$result = mysql_query($sql) or die("Could not run the query: " . mysql_error());
$rows = mysql_num_rows($result);
if ($rows == 0) {
    echo 'NO Updates in the Database right now';
}
else {
    while($result = mysql_fetch_array($result)) {
        echo 'Date:' .$result['date'];
        echo 'Time:' .$result['time'];
    }
}
?>
i ran that query and it spit out:
Could not run the query: No database selected
i know it sounds very simple but i have everything correct in my sql config file. i am absoulutly 100% sure about this,but here it is anyways.

Code: Select all

<?PHP

/******************************
SQL Connection Files
Edit what you want here. 
It will be included in other files for connecting.
******************************/

$host = "localhost";
$user = "***l"; //sql username
$pass = "*pass*"; //sql password
$db = "**DB**" ; //database name
mysql_connect($host,$user,$pass);
mysql_select_db($db) or mysql_error();
?>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Code: Select all

mysql_connect($host,$user,$pass) or die('Could not connect: '.mysql_error()); 
mysql_select_db($db) or die('Could not select DB: '.mysql_error());
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok,i found the problem,it was just in cpanel...i forgot to add the sql user...sorry all,but thanks for hlep
Post Reply