Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_num_rows();

Status
Not open for further replies.

pollock77

Programmer
Aug 9, 2004
25
US
I can't get this function to work. A sample code:

Code:
<?

$link = mysql_connect("localhost", "users", "users");
mysql_select_db("users", $link);

$result = mysql_query("SELECT * FROM user", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";
?>


And I'm getting the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/pollock7/public_html/new/hell.php on line 7
Rows


Why is this happening?
 
There's some error happening somewhere.

As a first step, I recommend you change

$result = mysql_query("SELECT * FROM user", $link);

to read

$result = mysql_query("SELECT * FROM user", $link) [red]or die(mysql_error())[/red];


Want the best answers? Ask the best questions!

TANSTAAFL!!
 

sleipnir214

Out of curiosity - what do you do to handle errors when you're designing php-database communications?

Do you use "or die" to trap errors? Or do you test for errors and then handle them quietly? I would assume the latter, but I've noticed you mention the "or die" situation on several occasions, and I am just wondering if you use it as a method of testing, or for actual web development.

I've come upon an understanding in my own demented mind that the "or die" use is not safe - as errors could display table information.

...just curious...

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
You're right -- use of "or die" is unsafe in a production environment.

The user should never see error messages -- the script should gracefully handle errors. I use a variety of methods, depending on context.

However, "or die" is perfectly acceptible for use in a development environment. And that's what the questions in here constitute.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Just out of interest I use @ and handle the error. I know this will not go down well in some quarters but I like to operate at that level of control.
(I should mention I don't actually write PHP)
 
That's what I do as well. Something like:

[tt]
$result = @mysql_query($sql);
if (!$result) {
echo "An error has occurred. Please try again later.";
processError($mysql_errno());
} else {
// continue processing...
}
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
The "@" operator doesn't really handle any errors. It just supresses the output of error messages.

There are times where this is necessary. Some functions, regardless of how you trap errors, will output some message when an error occurs. In those cases, you need the "@".

In general, though, "@" is the moral equivalent of sweeping dirt under the rug.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's why I said "some quarters" !,
I think sleipnir hits the nail on the head by saying it doesn't really handle errors. That's correct, I want to handle the errors (perhaps when i go up to 5.0 I'll change). I would go one further than clflava by checking mysql_errorno rather than checking the result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top