RE: [PHP] sql query successful
-Original Message- From: Tyler Longren [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 18, 2001 1:25 PM To: php-general Subject: [PHP] sql query successful I've been writing database enabled site for quite a while now. One thing I have never figured out it how to determine if a sql query is successful. This works: if ($connection = mysql_connect(host,username,password)) { print Successful connection: } else { print No connection; } But this won't work: if ($sql = mysql_query(SELECT * FROM table ORDER BY rand())) { print SQL executed successfully.; } else { print SQL not executed successfully.; } In what way doesn't this work? The above code should tell you that the query was properly formed and error-free, and was cheerfully accepted and processed by the database server. (You don't get much more successful than that...) If what you're really looking for is whether there were any rows returned by the query, then you'll want to look into the mysql_num_rows() function. --- Mark Roedel ([EMAIL PROTECTED]) || There cannot be a crisis next week. Systems Programmer / WebMaster || My schedule is already full. LeTourneau University ||-- Henry Kissinger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] sql query successful
I believe you should use the error function for the database you're using to check for failed queries. If you're using MySQL (which it appears that you are), you would do something like this: $sql = mysql_query(SELECT * FROM table ORDER BY rand()); if(mysql_error()) echo SQL not executed successfully.; else print SQL executed successfully.; Josh Hoover KnowledgeStorm, Inc. [EMAIL PROTECTED] Searching for a new IT solution for your company? Need to improve your product marketing? Visit KnowledgeStorm at www.knowledgestorm.com to learn how we can simplify the process for you. KnowledgeStorm - Your IT Search Starts Here This works: if ($connection = mysql_connect(host,username,password)) { print Successful connection: } else { print No connection; } But this won't work: if ($sql = mysql_query(SELECT * FROM table ORDER BY rand())) { print SQL executed successfully.; } else { print SQL not executed successfully.; } Can anyone tell me how I can tell if the SQL executed successfully? Is there any method besides OR DIE?
Re: [PHP] sql query successful
What about when DELETING from a table??? It always returns true. Example...the highest ID in this table is 10: ? if ($connection = mysql_connect(localhost,mysql,mysql)) { print Connection established; $db = mysql_select_db(aanr, $connection); if ($sql = mysql_query(DELETE FROM bullitens WHERE id='1212')) { print Successful; } else { print Not successful.; } } else { print Connection NOT established.; } ? We try to delete the bulliten with id of 1212, but there is not bulliten with that ID, and it says Successful. I know I could use mysql_affected_rows(), but why doesn't this work??? Tyler - Original Message - From: Mark Roedel [EMAIL PROTECTED] To: Tyler Longren [EMAIL PROTECTED]; php-general [EMAIL PROTECTED] Sent: Wednesday, July 18, 2001 1:30 PM Subject: RE: [PHP] sql query successful -Original Message- From: Tyler Longren [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 18, 2001 1:25 PM To: php-general Subject: [PHP] sql query successful I've been writing database enabled site for quite a while now. One thing I have never figured out it how to determine if a sql query is successful. This works: if ($connection = mysql_connect(host,username,password)) { print Successful connection: } else { print No connection; } But this won't work: if ($sql = mysql_query(SELECT * FROM table ORDER BY rand())) { print SQL executed successfully.; } else { print SQL not executed successfully.; } In what way doesn't this work? The above code should tell you that the query was properly formed and error-free, and was cheerfully accepted and processed by the database server. (You don't get much more successful than that...) If what you're really looking for is whether there were any rows returned by the query, then you'll want to look into the mysql_num_rows() function. --- Mark Roedel ([EMAIL PROTECTED]) || There cannot be a crisis next week. Systems Programmer / WebMaster || My schedule is already full. LeTourneau University ||-- Henry Kissinger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sql query successful
Tyler Longren pressed the little lettered thingies in this order... Hello everyone, I've been writing database enabled site for quite a while now. One thing I have never figured out it how to determine if a sql query is successful. This works: if ($connection = mysql_connect(host,username,password)) { print Successful connection: } else { print No connection; } But this won't work: if ($sql = mysql_query(SELECT * FROM table ORDER BY rand())) { print SQL executed successfully.; } else { print SQL not executed successfully.; } Can anyone tell me how I can tell if the SQL executed successfully? Is there any method besides OR DIE? What's wrong with or die(... ? If you get no error, the query was successful. If you take your $sql = out it *should* work. mysql_query returns TRUE or FALSE depending on whether or not the query succeeded. This isn't tested, but it should work: if (mysql_query(SELECT * FROM table ORDER BY rand())) { echo Success!; } else { echo Failure!; } Your statement above is checking to see if the fact that $sql is equal to mysql_query(SELECT * FROM table ORDER BY rand()) is TRUE, but you're using an assignment operator (=) and not a comparison operator (==), so it should always return FALSE. (Can you use an assignment operator in an if() statement?) Christopher Ostmo a.k.a. [EMAIL PROTECTED] AppIdeas.com Innovative Application Ideas Meeting cutting edge dynamic web site needs since the dawn of Internet time (1995) Business Applications: http://www.AppIdeas.com/ Open Source Applications: http://open.AppIdeas.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] sql query successful
-Original Message- From: Christopher Ostmo [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 18, 2001 2:43 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] sql query successful Your statement above is checking to see if the fact that $sql is equal to mysql_query(SELECT * FROM table ORDER BY rand()) is TRUE, but you're using an assignment operator (=) and not a comparison operator (==), so it should always return FALSE. (Can you use an assignment operator in an if() statement?) Actually, you can. (See http://www.php.net/manual/en/language.operators.assignment.php) An assignment operation will return the value that was assigned. Thus, if we have if ($sql = mysql_query($query_string)) and the mysql_query call returns a non-false value, the entire expression will evaluate to true. --- Mark Roedel | The most overlooked advantage to owning a Systems Programmer| computer is that if they foul up there's no LeTourneau University | law against whacking them around a little. Longview, Texas, USA | -- Owen Porterfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sql query successful
Tyler Longren pressed the little lettered thingies in this order... What about when DELETING from a table??? It always returns true. Example...the highest ID in this table is 10: ? if ($connection = mysql_connect(localhost,mysql,mysql)) { print Connection established; $db = mysql_select_db(aanr, $connection); if ($sql = mysql_query(DELETE FROM bullitens WHERE id='1212')) { print Successful; } else { print Not successful.; } } else { print Connection NOT established.; } ? We try to delete the bulliten with id of 1212, but there is not bulliten with that ID, and it says Successful. I know I could use mysql_affected_rows(), but why doesn't this work??? mysql_query() is not intended to be used as a means by which you can check the number of rows that are affected. If it returned FALSE severy time your query was empty, you would have to rewrite all of your of code to eliminate errors. It only checks for the validity of your query. To ask why this doesn't work would be the same thing as asking why you can type this query from the command-line mysql client and the query succeeds without an error. It succeeds and says 0 rows. It's a valid query with zero results. A FALSE return from a programming function indicates an error condition, not the lack of results. Christopher Ostmo a.k.a. [EMAIL PROTECTED] AppIdeas.com Innovative Application Ideas Meeting cutting edge dynamic web site needs since the dawn of Internet time (1995) For a good time, http://www.AppIdeas.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] sql query successful
Mark Roedel pressed the little lettered thingies in this order... -Original Message- From: Christopher Ostmo [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 18, 2001 2:43 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] sql query successful Your statement above is checking to see if the fact that $sql is equal to mysql_query(SELECT * FROM table ORDER BY rand()) is TRUE, but you're using an assignment operator (=) and not a comparison operator (==), so it should always return FALSE. (Can you use an assignment operator in an if() statement?) Actually, you can. (See http://www.php.net/manual/en/language.operators.assignment.php) An assignment operation will return the value that was assigned. Thus, if we have if ($sql = mysql_query($query_string)) and the mysql_query call returns a non-false value, the entire expression will evaluate to true. Thank you. I've never tried to assign within an if() clause. You learn 13 or 14 new things every day... Christopher Ostmo a.k.a. [EMAIL PROTECTED] AppIdeas.com Innovative Application Ideas Meeting cutting edge dynamic web site needs since the dawn of Internet time (1995) For a good time, http://www.AppIdeas.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] sql query successful
-Original Message- From: Tyler Longren [mailto:[EMAIL PROTECTED]] Sent: Wednesday, July 18, 2001 1:36 PM To: Mark Roedel; php-general Subject: Re: [PHP] sql query successful What about when DELETING from a table??? It always returns true. Example...the highest ID in this table is 10: ? if ($connection = mysql_connect(localhost,mysql,mysql)) { print Connection established; $db = mysql_select_db(aanr, $connection); if ($sql = mysql_query(DELETE FROM bullitens WHERE id='1212')) { print Successful; } else { print Not successful.; } } else { print Connection NOT established.; } ? We try to delete the bulliten with id of 1212, but there is not bulliten with that ID, and it says Successful. I know I could use mysql_affected_rows(), but why doesn't this work??? Because the query itself is still valid. No syntax errors, no connection errors, etc. The fact that there wasn't any data that needed to be deleted doesn't affect the validity of the query; just the results of it. Basically, what it boils down to is that There wasn't anything for me to do is not an error. It's a perfectly valid result. :) --- Mark Roedel ([EMAIL PROTECTED]) || There cannot be a crisis next week. Systems Programmer / WebMaster || My schedule is already full. LeTourneau University ||-- Henry Kissinger -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]