Solved

mysql_num_rows() error...

Posted on 2011-03-25
5
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

718 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