RE: [PHP] RE: [PHP-DB] Best way to check if a query succeeded
Wouldn't doing it like this however be ok: if ($result = mysql_query($query)) { I could be wrong, but I do not believe so. The above would evaluate as _true_ 100% of the time because irregardless of success of the query, $result is getting a value. It's just like doing: $i = "joe"; if( $i = "bob" ) { // this will always run. } Again, I could be wrong but don't think I am. Chris
Re: [PHP] RE: [PHP-DB] Best way to check if a query succeeded
On Friday 30 March 2001 16:25, you wrote: Wouldn't doing it like this however be ok: if ($result = mysql_query($query)) { I could be wrong, but I do not believe so. The You are wrong :) ($result = mysql_query(...)) assigns a value to result, and the entire expression evaluates to that value (i.e. the return value of mysql_query). if( $i = "bob" ) { // this will always run. yes, because "bob" evaluates to true :) } -- Christian Reiniger LGDC Webmaster (http://sunsite.dk/lgdc/) Even idiots can handle computers, and many do. -- 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] RE: [PHP-DB] Best way to check if a query succeeded
Doesn't the command return 1 or 0 in success or failure? Not 1 or 0. It returns 0 or some other value. Almost the same, but not quite. You may not have a result Probably wrong but something like if (mysql_query($query)) { } else { } or you could die out mysql_query($query) or die You could do that. But once you run the query, that's it. You'll have nothing to use with any of the other mysql functions because you need to pass to them the value that mysql() returns. So while the above might tell you whether or not the query succeeded, that's all you would get out of it. Wouldn't doing it like this however be ok: if ($result = mysql_query($query)) { ; success, have $result if (mysql_num_rows($result) 0) { ; have data to play with } else { ; dont } } else { ; wasn't successful, don't have result } ? -- 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]
[PHP] Re: [PHP-DB] Re: [PHP] RE: [PHP-DB] Best way to check if a query succeeded
On Thursday, March 29, 2001, at 08:32 AM, Nick Davies wrote: Doesn't the command return 1 or 0 in success or failure? You may not have a result Probably wrong but something like if (mysql_query($query)) { } else { } What I understand is that the return from the query only tells you if the syntax is correct. NOT whether there were any records returned. That is what the mysql_num_rows gets you-- the number of records returned. Terry -- 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] RE: [PHP-DB] Best way to check if a query succeeded
Doesn't the command return 1 or 0 in success or failure? You may not have a result Probably wrong but something like if (mysql_query($query)) { } else { } or you could die out mysql_query($query) or die On Thu, 29 Mar 2001, Boget, Chris wrote: i was just wondering what you guys do to check if a wquery suceeded or not? I know about mysql_num_rows() and mysql_affected_rows(), just wondered what you guys do? I do this: $result = mysql( $dbname, $query ); if(( $result ) ( mysql_errno() == 0 )) { // query was successful if( mysql_num_rows( $result ) 0 ) { // do your stuff here... } else { echo "Successful query returned no rows"; } } else { echo "Query failed: " . mysql_error() . "br\n"; } There is a difference between a "successful query" and a query that returns 0 rows and should be handled differently, typically. Depending on what you are doing, you might not care -- 0 rows = unsuccessful query. However, in my case, I want to know if a query was malformed, contained erroneous data that's throwing mySQL off or whatever. These types of errors will return 0 rows as well. Chris -- 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] RE: [PHP-DB] Best way to check if a query succeeded
Doesn't the command return 1 or 0 in success or failure? Not 1 or 0. It returns 0 or some other value. Almost the same, but not quite. You may not have a result Probably wrong but something like if (mysql_query($query)) { } else { } or you could die out mysql_query($query) or die You could do that. But once you run the query, that's it. You'll have nothing to use with any of the other mysql functions because you need to pass to them the value that mysql() returns. So while the above might tell you whether or not the query succeeded, that's all you would get out of it. Chris