?
Solved

mysql_num_rows() error...

Posted on 2011-03-25
5
Medium Priority
?
453 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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