how can I check a query is successful or not?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

how can I check a query is successful or not?

Post 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	');
		}
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

That should work just fine. I've never used () with echo. I'm not sure how it affected it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

How does it not work?
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

It simply does not output anything.When I check my db, its getting updated. :(
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

Man!! it worked perfect.
UUUUUUUUMMMMMMMMHHHHHHHHHHaaa Everah

Thank you
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

Thank you buddy, thats best practice.I will do as soon as I finish the framework first.You know dead lines.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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())..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
Post Reply