Connecting Tech Pros Worldwide Forums | Help | Site Map

mysql_query return value

Bruce A. Julseth
Guest
 
Posts: n/a
#1: Jul 17 '05
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




Tom Thackrey
Guest
 
Posts: n/a
#2: Jul 17 '05

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)
Bruce A. Julseth
Guest
 
Posts: n/a
#3: Jul 17 '05

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]


OSIRIS
Guest
 
Posts: n/a
#4: Jul 17 '05

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.

Gordon Burditt
Guest
 
Posts: n/a
#5: Jul 17 '05

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
Closed Thread