954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Mysql_num_rows($result)

please help me
when i run my code
by using Mysql_num_rows()
Warning: Supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\php\connect to database.php on line 13

this is my code

<?php
/* declar some relevant variables */
$Host = "localhost"; //location of mySQL on server
$Table = "books"; //name of the table within the database

mysql_connect ($Host,$Table) or
die ("unable to connect to database");
//mysql_select_db("$Name") or
//die ("unable to select DB");
$sqlquery = "SELECT * FROM $Table";
$result = Mysql_query($sqlquery);
$number = Mysql_num_rows($result);


?>

what shall i do

:confused:

akkoub
Newbie Poster
1 post since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

[php]<?
$host = "localhost";
$user = "me"
$pass = "super-secret";
$database = "db";
$table = "books";

//you need to put them in this order, the table name does not go here!
$db = mysql_connect ($host, $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());

//you must select a db, as all tables are in a database
mysql_select_db($db);

$sql_query = "SELECT * FROM $Table";
$result = mysql_query($sql_query);
if ($result == 0)
{
echo "I had a problem running the query!";
$number = 0;
}
else
{
echo "my sql query ran sucsessfully";
$number = mysql_num_rows($result);
}
mysql_close();
?>[/php]

you'll notice i put the mysql error number in case there was an error opening the database, I also checked to make sure usign the if statement, add all this extra junk to see where you are going wrong, and then once you figure it out then just clean it up a bit

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

As paradox has shown with his example, you left out the database, varible so mysql won't know what database it needs to get its data from.

Zachery
The Geek Father
Team Colleague
894 posts since Nov 2003
Reputation Points: 96
Solved Threads: 21
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You