473,835 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='passw ord' where Customer='Smith ';

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

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

$Result = mysql_query($sq l, $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
4 15774

On 16-Nov-2003, "Bruce A. Julseth" <br*****@attglo bal.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='passw ord' where Customer='Smith ';

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

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

$Result = mysql_query($sq l, $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*********@wil lglen.net (it's reserved for spammers)
Jul 17 '05 #2
Thanks... Works like a champ....

Bruce

"Tom Thackrey" <us***********@ nospam.com> wrote in message
news:ZM******** **********@news svr29.news.prod igy.com...

On 16-Nov-2003, "Bruce A. Julseth" <br*****@attglo bal.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='passw ord' where Customer='Smith ';
$Connection = mysql_connect(" localhost", "User", "PW") Or
die(mysql_error ());

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

$Result = mysql_query($sq l, $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*********@wil lglen.net (it's reserved for spammers)

Jul 17 '05 #3
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='passw ord' where Customer='Smith ';

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

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

$Result = mysql_query($sq l, $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='passw ord' where Customer='Smith ';

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

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

$Result = mysql_query($sq l, $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
>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='passw ord' where Customer='Smith ';
$Connection = mysql_connect(" localhost", "User", "PW") Or
die(mysql_erro r());
$DB = mysql_select_db ("Database", $Connection);
$Result = mysql_query($sq l, $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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8785
by: lkrubner | last post by:
www.php.net says: >>>>>>>>>>>> Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query() returns TRUE on success and FALSE on error. A non-FALSE return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It...
6
4675
by: aa | last post by:
I use the following fragment of code to output datf from MySQL: ====================================================== $chan = mysql_connect ($db_host, $username, $password); mysql_select_db ($DB_name, $chan); $resultid = mysql_query ("select name_ru, description_ru, retail, dealer from lasershot WHERE le='1'", $chan); ........ ======================================================
0
2007
by: Bill | last post by:
I have the following line in a php script $queryDeletePosition = mysql_query("DELETE FROM `Position` WHERE `stockID` = '$existStockID' AND `userID`='$userID' AND `groupID`='$groupID' LIMIT 1 ", $connMydb) or die(mysql_error()); It works fine . The entry I want deleted from the database is deleted. However $queryDeletePosition value is 1, confirmed by echo"<script> alert(\" result is $queryDeletePosition \"); </script>"; And after ...
7
3929
by: John Moore | last post by:
Hi I posted three days ago about a function I wrote which kept refusing to run a mysql_query. Well I did away with the function, and hard coded the variables into the query itself. Here's what I have now: mysql_query("UPDATE about SET category_id = 58 WHERE about_id = 5") or die('error: ' . mysql_error());
0
1146
by: FB2006 TEAM | last post by:
Apache 2.0 PHP 4.4 MySQL 5.0 ========= Ok, here what is going on! Every time when I trying to login into system I’m getting message (its last “else” in F_LogIn function): “Nothing is working” and so result of this: “You could not be logged in” I don’t know why it’s happen, but by testing my query, I found out that problem in “user_password”, without this parameter everything is working smoothly. To be more precise, the query is not...
5
2656
SHOverine
by: SHOverine | last post by:
Hello All. I have a problem with a form that I am trying to create. What happens is: I have a person enter a UserName and select some values using radio buttons. They submit the form, and the next page is intended to echo their information back. I was able to get the first page to write values to the database, but when I add the second page, the values do not write to the database and I get an error. Here is a pared-down version of the...
4
5161
by: atyndall | last post by:
OK, this is the relevant portion script: <?php $username = '__'; // MySQL Database Username. $password = '__'; // MySQL Database Password. $server = '__'; // MySQL Database server (most likely localhost).
6
10990
by: aerobat | last post by:
hello all just wondering if anyone can help me with this problem. Ii has to do with a simple mysql_query. i have tried so many different ways of doing it, but it wont work. here is the code: #include <stdio.h> #include <math.h> #include <string.h> #include <mysql.h> main() {
5
9148
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php $conn = @mysql_connect($db_server,$db_user,$db_pass) or die("Connection to Database Server Failed"); @mysql_select_db($db) or die("Database Selection Failed"); return $conn; }
0
9803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4433
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.