Solved

mysql_num_rows() error...

Posted on 2011-03-25
5
438 Views
Last Modified: 2012-06-27
I'm trying to return a number of records in a table using mysql_num_rows();

I'm having issues however, I get the following message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\....

For the sake of learning PHP i simply copied this code from a tutorial and plugged my info in, thinking I'd get it to work and disect it.  No such luck.

I've been able to run insert statements, but for some reason the mysql_num_rows() isn't working.  The code I use is posted below.

Any ideas?  THANKS!
<?php
$dbhost = 'my.remote.mysqlserver';
$dbuser = 'myusername';
$dbpass = 'mypassword';
$dbname = 'mydatabase';


$link = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $link);

$result = mysql_query("SELECT * FROM myTable", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

Open in new window

0
Comment
Question by:slightlyoff
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35217789
Did you also test it with the database configuration (host, user, pass, name) like above?
Because you should have a existing database to connect to before this can work.
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 35217910
i was able to do an insert using the configuration above... is that what you mean?
0
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 250 total points
ID: 35217985
Try this,
this should give you some helpfull error messages when they are thrown.

<?php
$dbhost = 'my.remote.mysqlserver';
$dbuser = 'myusername';
$dbpass = 'mypassword';
$dbname = 'mydatabase';


$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $link);

$result = mysql_query("SELECT * FROM myTable", $link);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

Open in new window

0
 
LVL 5

Assisted Solution

by:tsmgeek
tsmgeek earned 250 total points
ID: 35217990
put some error catching in your code to look if the problem is the db connection

<?php
$dbhost = 'my.remote.mysqlserver';
$dbuser = 'myusername';
$dbpass = 'mypassword';
$dbname = 'mydatabase';


$link = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}


$result = mysql_query("SELECT * FROM myTable", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 35218144
I'm a moron...  thanks for your help!  I was able to track down the problem... the table had been renamed, after I had tested the insert statments...

Thanks for the example code!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question