successful query?

This is a discussion on "successful query?" within the PHP Forum section. This forum, and the thread "successful query? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 31st, 2007, 14:48
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
successful query?

to check if a query was succesful, is the php if statement just;

PHP: Select all

if (mysql_query($query) or die ($error->error()) {
// do this if it happened
} else {
//if it didn't

The or die bit refers to my errors class
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote

  #2 (permalink)  
Old Aug 31st, 2007, 15:19
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: successful query?

Quote:
PHP: Select all

if (mysql_query($query) or die ($error->error()) {
// do this if it happened
} else {
//if it didn't


hmmm.... I didn't test it, but I think it should work.

mysql_query() returns either the results if successful, or FALSE if not.

die() does not return any values, so I think it should work!

Although, maybe this would be a better way of writing it?:

PHP: Select all

if($result=mysql_query($query))
{
    
// do this if it happened
}
else
{
    die(
$error->error());

Guess it's just what's easiest to read for you...
Reply With Quote
  #3 (permalink)  
Old Aug 31st, 2007, 15:25
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: successful query?

PHP: Select all

if($result=mysql_query($query) or die ($error->error())
{
    
// do this if it happened
}
else
{
    die(
$error->error());

would that work aswell?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #4 (permalink)  
Old Aug 31st, 2007, 15:27
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: successful query?

The die statement will always take precedence when the mysql query fails. This means that, if mysql_query returns false, then die(...) is executed, ending the script immediately.

The else part of the if statement will then never be executed.
Reply With Quote
  #5 (permalink)  
Old Aug 31st, 2007, 15:31
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: successful query?

Quote:
Originally Posted by jimz View Post
die() does not return any values, so I think it should work!
die() never returns at all. It's a synonym for exit() which exits the script immediately without running another line of code, except for the destructors of already existing objects.
Reply With Quote
  #6 (permalink)  
Old Aug 31st, 2007, 15:33
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: successful query?

sorry...
i just realised my mistake.
thanks guys
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #7 (permalink)  
Old Aug 31st, 2007, 17:16
Junior Member
Join Date: Jan 2007
Location: South Cambs, UK
Age: 18
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to afmanuk
Re: successful query?

Thing is, if($result=mysql_query($query)) ...
Will always return true as the return value (true, false, or result set) will be set to the $result variable.
I think that
PHP: Select all

if(!($result mysql_query($query))) {
//Failure stuff first
} else {
//Success stuff here

This should be better as it will always return false on error, but will only return true if the query is UPDATE, DELETE, DROP, etc...
Reply With Quote
  #8 (permalink)  
Old Aug 31st, 2007, 17:20
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: successful query?

what about
PHP: Select all

$result mysql_query($query);
if (!
$result) {
//success
} else {
//fail

?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #9 (permalink)  
Old Aug 31st, 2007, 17:22
Junior Member
Join Date: Jan 2007
Location: South Cambs, UK
Age: 18
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to afmanuk
Re: successful query?

Success and fail the other way round, cause you're checking whether $result is false. But yeah... That's good
Reply With Quote
  #10 (permalink)  
Old Aug 31st, 2007, 18:44
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: successful query?

cheers
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
Reply

Tags
mysql, php, query

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Happy successful 2008! from Cratima. Cratima Webforumz Cafe 1 Jan 9th, 2008 00:28
Direct Marketing Manager (B2B) for large and successful online directory Web JobBot Job Opportunities 0 Oct 9th, 2006 11:42


All times are GMT. The time now is 11:45.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43