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 4 15671
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)
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)
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.
>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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by lkrubner |
last post: by
|
6 posts
views
Thread by aa |
last post: by
|
reply
views
Thread by Bill |
last post: by
|
7 posts
views
Thread by John Moore |
last post: by
| | | | | | | | | | | | | | | |