Page 1 of 1

how can I check a query is successful or not?

Posted: Fri Sep 15, 2006 2:31 pm
by saumya
Hi,
can u please explain, how can I check whether my query has inserted data or failed?I have the code below now, but that does not work :(

Code: Select all

$result=mysql_query($query);
		if($result){
			echo('Successfully updaed');
		}else{
			echo('Could not put your data into database	');
		}

Posted: Fri Sep 15, 2006 2:39 pm
by wtf
That should work just fine. I've never used () with echo. I'm not sure how it affected it.

Posted: Fri Sep 15, 2006 2:39 pm
by feyd
How does it not work?

Posted: Fri Sep 15, 2006 2:41 pm
by saumya
It simply does not output anything.When I check my db, its getting updated. :(

Posted: Fri Sep 15, 2006 2:42 pm
by RobertGonzalez

Code: Select all

<?php
if (!$result = mysql_query($query))
{
    die('Could not execute the query: ' . mysql_error());
}
?>
EDIT | This came in while I was posting thread...
saumya wrote:It simply does not output anything.When I check my db, its getting updated. :(
Are you doing an insert/update query?

Posted: Fri Sep 15, 2006 2:48 pm
by saumya
Man!! it worked perfect.
UUUUUUUUMMMMMMMMHHHHHHHHHHaaa Everah

Thank you

Posted: Fri Sep 15, 2006 3:20 pm
by Christopher
Except you really should not use die() for error handling. The code should check for errors and respond with a page with an error message, do a redirect, etc.

Posted: Fri Sep 15, 2006 3:24 pm
by saumya
Thank you buddy, thats best practice.I will do as soon as I finish the framework first.You know dead lines.

Posted: Fri Sep 15, 2006 3:46 pm
by RobertGonzalez
arborint wrote:Except you really should not use die() for error handling. The code should check for errors and respond with a page with an error message, do a redirect, etc.
Sorry, die was for testing/speed in response to your question. For real life you should develop a custom error handler (that does not repeat the mysql_error())..

Posted: Fri Sep 15, 2006 3:47 pm
by Chris Corbyn
arborint wrote:Except you really should not use die() for error handling. The code should check for errors and respond with a page with an error message, do a redirect, etc.
Now a question. I recently started throwing exceptions (of various types such as DBException, UserException etc..) that actually implement a template approach with a render() method. They look pretty. It's still an error dumped to screen but it looks nice and if you're logged in as a developer you even get a little geshi output with a code excerpt and the relevant line highlighted. Bad use of exception handling?

Posted: Fri Sep 15, 2006 3:56 pm
by onion2k
arborint wrote:Except you really should not use die() for error handling. The code should check for errors and respond with a page with an error message, do a redirect, etc.
You should also be wrapping the SQL statement in a transaction and rolling back after the failed statement.