By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

mysql_query return value

P: n/a
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

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

On 16-Nov-2003, "Bruce A. Julseth" <br*****@attglobal.net> wrote:
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?


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 ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

P: n/a
Thanks... Works like a champ....

Bruce

"Tom Thackrey" <us***********@nospam.com> wrote in message
news:ZM******************@newssvr29.news.prodigy.c om...

On 16-Nov-2003, "Bruce A. Julseth" <br*****@attglobal.net> wrote:
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?


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 ja*********@willglen.net (it's reserved for spammers)

Jul 17 '05 #3

P: n/a
Bruce A. Julseth wrote:
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
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:
$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>";

}
}


Try it maybe it's your answer.

Jul 17 '05 #4

P: n/a
>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.
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.

I thought mysql_query returned false if a query failed. The test fail path
is never taken in the following code is never taken.
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.
Also, I get an invalid argument on mysql_num_rows($Result) error message.
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.)
$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?

Gordon L. Burditt
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.