Solved

mysql_num_rows() error...

Posted on 2011-03-25
5
428 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now