Connecting Tech Pros Worldwide Help | Site Map

mysql_query return value

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:10 AM
Bruce A. Julseth
Guest
 
Posts: n/a
Default mysql_query return value

I know the following $sql will fail since there is no Customer='Smith'. I
want to determine how to test a failure of mysql_query.

I thought mysql_query returned false if a query failed. The test fail path
is never taken in the following code is never taken.

Also, I get an invalid argument on mysql_num_rows($Result) error message.

$sql = "Update Table Set Password='password' where Customer='Smith';

$Connection = mysql_connect("localhost", "User", "PW") Or
die(mysql_error());

$DB = mysql_select_db("Database", $Connection);

$Result = mysql_query($sql, $Connection) or die(mysql_error());

if(!$Result) {

echo "Fail<br>";

}

$Rows = mysql_num_rows($Result) or die(mysql_error());

if($Rows > 0) {

echo "Success Rows: $Rows<br>";

} else {

echo "Failure<br>";

}

What do I need to do to test for a mysql_query failure?

Thank you...

Bruce




  #2  
Old July 17th, 2005, 01:10 AM
Tom Thackrey
Guest
 
Posts: n/a
Default Re: mysql_query return value


On 16-Nov-2003, "Bruce A. Julseth" <bruceaj@attglobal.net> wrote:
[color=blue]
> I know the following $sql will fail since there is no Customer='Smith'. I
> want to determine how to test a failure of mysql_query.
>
> I thought mysql_query returned false if a query failed. The test fail path
> is never taken in the following code is never taken.
>
> Also, I get an invalid argument on mysql_num_rows($Result) error message.
>
> $sql = "Update Table Set Password='password' where Customer='Smith';
>
> $Connection = mysql_connect("localhost", "User", "PW") Or
> die(mysql_error());
>
> $DB = mysql_select_db("Database", $Connection);
>
> $Result = mysql_query($sql, $Connection) or die(mysql_error());
>
> if(!$Result) {
>
> echo "Fail<br>";
>
> }
>
> $Rows = mysql_num_rows($Result) or die(mysql_error());
>
> if($Rows > 0) {
>
> echo "Success Rows: $Rows<br>";
>
> } else {
>
> echo "Failure<br>";
>
> }
>
> What do I need to do to test for a mysql_query failure?[/color]

The query will not 'fail' it will not affect any rows. Use
mysql_affected_rows() and check for zero.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
  #3  
Old July 17th, 2005, 01:10 AM
Bruce A. Julseth
Guest
 
Posts: n/a
Default Re: mysql_query return value

Thanks... Works like a champ....

Bruce

"Tom Thackrey" <use.signature@nospam.com> wrote in message
news:ZMYtb.10650$0E6.9470@newssvr29.news.prodigy.c om...[color=blue]
>
> On 16-Nov-2003, "Bruce A. Julseth" <bruceaj@attglobal.net> wrote:
>[color=green]
> > I know the following $sql will fail since there is no Customer='Smith'.[/color][/color]
I[color=blue][color=green]
> > want to determine how to test a failure of mysql_query.
> >
> > I thought mysql_query returned false if a query failed. The test fail[/color][/color]
path[color=blue][color=green]
> > is never taken in the following code is never taken.
> >
> > Also, I get an invalid argument on mysql_num_rows($Result) error[/color][/color]
message.[color=blue][color=green]
> >
> > $sql = "Update Table Set Password='password' where[/color][/color]
Customer='Smith';[color=blue][color=green]
> >
> > $Connection = mysql_connect("localhost", "User", "PW") Or
> > die(mysql_error());
> >
> > $DB = mysql_select_db("Database", $Connection);
> >
> > $Result = mysql_query($sql, $Connection) or die(mysql_error());
> >
> > if(!$Result) {
> >
> > echo "Fail<br>";
> >
> > }
> >
> > $Rows = mysql_num_rows($Result) or die(mysql_error());
> >
> > if($Rows > 0) {
> >
> > echo "Success Rows: $Rows<br>";
> >
> > } else {
> >
> > echo "Failure<br>";
> >
> > }
> >
> > What do I need to do to test for a mysql_query failure?[/color]
>
> The query will not 'fail' it will not affect any rows. Use
> mysql_affected_rows() and check for zero.
>
> --
> Tom Thackrey
> www.creative-light.com
> tom (at) creative (dash) light (dot) com
> do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)[/color]


  #4  
Old July 17th, 2005, 01:17 AM
OSIRIS
Guest
 
Posts: n/a
Default Re: mysql_query return value

Bruce A. Julseth wrote:
[color=blue]
> I know the following $sql will fail since there is no Customer='Smith'. I
> want to determine how to test a failure of mysql_query.
>
> I thought mysql_query returned false if a query failed. The test fail path
> is never taken in the following code is never taken.
>
> Also, I get an invalid argument on mysql_num_rows($Result) error message.
>
> $sql = "Update Table Set Password='password' where Customer='Smith';
>
> $Connection = mysql_connect("localhost", "User", "PW") Or
> die(mysql_error());
>
> $DB = mysql_select_db("Database", $Connection);
>
> $Result = mysql_query($sql, $Connection) or die(mysql_error());
>
> if(!$Result) {
>
> echo "Fail<br>";
>
> }
>
> $Rows = mysql_num_rows($Result) or die(mysql_error());
>
> if($Rows > 0) {
>
> echo "Success Rows: $Rows<br>";
>
> } else {
>
> echo "Failure<br>";
>
> }
>
> What do I need to do to test for a mysql_query failure?
>
> Thank you...
>
> Bruce
>
>
>[/color]
I think it's an error in your script:

you test this:
if(!$Result) {

echo "Fail<br>";

}

But if the $sql fails you continue the script with
$Rows = mysql_num_rows($Result) or die(mysql_error());
or the sql has failed.
You should use a else:
[color=blue]
> $sql = "Update Table Set Password='password' where Customer='Smith';
>
> $Connection = mysql_connect("localhost", "User", "PW") Or
> die(mysql_error());
>
> $DB = mysql_select_db("Database", $Connection);
>
> $Result = mysql_query($sql, $Connection) or die(mysql_error());
>
> if(!$Result) {
>
> echo "Fail<br>";
>
> }
>
> ELSE {
> $Rows = mysql_num_rows($Result) or die(mysql_error());
>
> if($Rows > 0) {
>
> echo "Success Rows: $Rows<br>";
>
> } else {
>
> echo "Failure<br>";
>
> }
> }[/color]

Try it maybe it's your answer.

  #5  
Old July 17th, 2005, 01:24 AM
Gordon Burditt
Guest
 
Posts: n/a
Default Re: mysql_query return value

>I know the following $sql will fail since there is no Customer='Smith'. I[color=blue]
>want to determine how to test a failure of mysql_query.[/color]

There are failures and then there are failures.

mysql_query will fail when it couldn't even DO the query for
one or more of various reasons:

- Syntax error in the query (e.g. "elect * from ... ")
- Reference to nonexistent tables or columns
- Permissions problems (e.g. UPDATE when you have read-only access to a table)
- Network trouble talking to the server (but to get this far, you did
successfully connect).
- All sorts of problems with corrupted tables and/or bad disk sectors.
and lots of other stuff I didn't think of.

[color=blue]
>I thought mysql_query returned false if a query failed. The test fail path
>is never taken in the following code is never taken.[/color]

If you consider UPDATE not changing anything to be a failure, you
can check mysql_affected_rows() after an UPDATE, DELETE, or INSERT.
Beware, however, that mysql_affected_rows() will return 0 if
Smith's password was already 'password'. Do you consider THAT to
be a "failure"? This tends to be a religious issue.
[color=blue]
>Also, I get an invalid argument on mysql_num_rows($Result) error message.[/color]

mysql_num_rows() only works after a query that returns a result set
(even a 0-row result set). PHP documents this as only working after
a SELECT, but MySQL says it works after any query that returns a
result set (SELECT, SHOW, DESCRIBE, EXPLAIN, and I'm not sure that's
all of them). In any case, it doesn't work after a query that does
not return a result set (INSERT, UPDATE, DELETE, ALTER TABLE, etc.)
[color=blue]
> $sql = "Update Table Set Password='password' where Customer='Smith';
> $Connection = mysql_connect("localhost", "User", "PW") Or
>die(mysql_error());
> $DB = mysql_select_db("Database", $Connection);
> $Result = mysql_query($sql, $Connection) or die(mysql_error());
> if(!$Result) {
> echo "Fail<br>";
> }
> $Rows = mysql_num_rows($Result) or die(mysql_error());
> if($Rows > 0) {
> echo "Success Rows: $Rows<br>";
> } else {
> echo "Failure<br>";
> }
> What do I need to do to test for a mysql_query failure?[/color]


Gordon L. Burditt
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.