Solved

mysql_num_rows() error...

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

867 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

22 Experts available now in Live!

Get 1:1 Help Now