Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Need MySQL Alternative To The Syntax "or die()"
bakr_2k5
post Nov 21 2006, 04:46 PM
Post #1


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Hello again smile.gif

I'm facing a problem with PHP and MySQL...
I want, when a MySQL error occurs, to let the script continue.

Here's the script:
CODE

$query = "SELECT * FROM menus ORDER BY id ASC";
$menus_result = mysql_query($query) or die("Error!");

while( $menu=mysql_fetch_array($menus_result) ) {
   echo $menu['name']."<br />";
}

Now if the table "menus" doesn't exist, this would echo "Error!" where it's placed and terminate the whole script. But I want it to echo "Error!" and skip the while table but letting everything behind it continue. Is there any way doing this? I'm not that good with MySQL in PHP so still learning.

Maybe there is a way using an if or a loop or so. But don't have a clue to implement it with
"$menus_result = mysql_query($query)"

Thank you in advance! wink.gif

Bakr_2k5

This post has been edited by bakr_2k5: Nov 21 2006, 05:40 PM
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 21 2006, 05:51 PM
Post #2


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



QUOTE(bakr_2k5 @ Nov 22 2006, 12:46 AM) *

Hello again smile.gif

I'm facing a problem with PHP and MySQL...
I want, when a MySQL error occurs, to let the script continue.

Here's the script:
CODE

$query = "SELECT * FROM menus ORDER BY id ASC";
$menus_result = mysql_query($query) or die("Error!");

while( $menu=mysql_fetch_array($menus_result) ) {
   echo $menu['name']."<br />";
}

Now if the table "menus" doesn't exist, this would echo "Error!" where it's placed and terminate the whole script. But I want it to echo "Error!" and skip the while table but letting everything behind it continue. Is there any way doing this? I'm not that good with MySQL in PHP so still learning.

Maybe there is a way using an if or a loop or so. But don't have a clue to implement it with
"$menus_result = mysql_query($query)"

Thank you in advance! wink.gif

Bakr_2k5


Here, just tested it, and works. I'm using php5 and mysql5 on winxp pro
CODE

<?php
$query = "SELECT * FROM menus ORDER BY id ASC";
$menus_result = mysql_query($query);

if ($menus_result)
{
    while( $menu=mysql_fetch_array($menus_result) ) {
       echo $menu['name']."<br />";
    }
}

echo "Next Job";
?>


You can skip the "or die("Error!");"
"die()", according to php's manual is equivalent to "exit()", thus it will terminates execution of the script, skipping the remaining.

What i've change is just remove the "or die" and added a check for the returned result from mysql_query(), if mysql_query() failed, nothing will be return into $menus_result, thus skipping the menu code.

I did try to use "is_null($menus_result)", but didn't work, not sure why. The if check is actually a good practice so you don't just sit there and wait for mysql_fetch_array to complaint bout the result -> "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in "

Good luck

This post has been edited by faulty.lee: Nov 21 2006, 05:53 PM
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 21 2006, 05:57 PM
Post #3


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



Yeah awesome, it works smile.gif
Thank you faulty.lee!

Well could think of it myself now I see it, but my mind isn't very clear today wink.gif

Anyways thank you! And problem solved biggrin.gif

Bakr_2k5

EDIT:
is_null($menus_result) works here! Using the latest XAMPP version on linux (Gentoo).
EDIT2:
is_null($menus_result) doesn't work wink.gif ... it gives always a FALSE! Strange huh.gif

This post has been edited by bakr_2k5: Nov 21 2006, 06:02 PM
Go to the top of the page
 
+Quote Post
faulty.lee
post Nov 21 2006, 06:29 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 500
Joined: 5-November 06
Member No.: 17,016
myCENTs:NEGATIVE[-20.12]



You're welcome.

Just now I did try

$menus_result = mysql_query($query) or NULL;

which is suppose to set NULL into $menus_result, but then is_null($menus_result) still return false.

I guess if($menus_result) is less picky. Anyway, glad it helps
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 21 2006, 06:48 PM
Post #5


Advanced Member
Group Icon

Group: Members
Posts: 189
Joined: 15-November 05
From: Inland from the Left Coast of Canada
Member No.: 9,627
myCENTs:62.43



QUOTE
I did try to use "is_null($menus_result)", but didn't work, not sure why.


The results from the query are going to be Boolean 'true' or boolean 'false', so it will never be 'null', which is "no value". A NULL is different than boolean false, zero or a blank space, too.
Go to the top of the page
 
+Quote Post
bakr_2k5
post Nov 21 2006, 07:41 PM
Post #6


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(jlhaslip @ Nov 21 2006, 06:48 PM) *

The results from the query are going to be Boolean 'true' or boolean 'false', so it will never be 'null', which is "no value". A NULL is different than boolean false, zero or a blank space, too.


This isn't completely true, I think.

"$menus_result = mysql_query($query) or NULL;"

$menus_result would be the output of mysql_query($query), but when mysql_query($query) fails it should pass NULL to $menus_result. Thus resulting in $menus_result = NULL!
CODE

$query = "SELECT * FROM non_existent ORDER BY id ASC";
$menu_result = mysql_query($query) or NULL;
echo $menu_result;    // Which echoes nothing at all, thus NULL!

if($menu_result == NULL) {
    echo "Yes";
} else {
    echo "No";
}


This results in a "Yes" so $menu_result is NULL. So "is_null($menu_result)" SHOULD return TRUE.

QUOTE
http://php.net/is_null
is_null -- Finds whether a variable is NULL

This means "is_null()" doesn't do what it should do!
Anyways we shouldn't trust on the name of a function as "is_null()".

Bakr_2k5

This post has been edited by bakr_2k5: Nov 21 2006, 07:44 PM
Go to the top of the page
 
+Quote Post
Hercco
post Nov 23 2006, 05:44 PM
Post #7


Super Member
Group Icon

Group: Members
Posts: 595
Joined: 4-September 04
Member No.: 228



Bakr_2k5, NULL is a bit different in databases and programming languages like PHP.

NULL in PHP, like in C means a pointer to "nothing" while NULL is databases basically is for indicating "no value set". It's also improtant to bear in mind that NULL is databases does not equal to emptry string.

So when you test
CODE

if ($mysql_result == NULL )


you are essentially testing if the handle to the query result is NULL. And if the query is performed and the result is returned to that variable it is never NULL regardless of the query result.

Database can return a NULL for a value of a field.




This post has been edited by Hercco: Nov 23 2006, 05:49 PM
Go to the top of the page
 
+Quote Post
Quatrux
post Nov 23 2006, 07:06 PM
Post #8


the Q
Group Icon

Group: [HOSTED]
Posts: 1,133
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059
myCENTs:5.70



If you really want to avoid this kind of stuff I suggest you to create a class usually called Database and create functions there which would work together and you won't ever need to use or die and never will have problems, of course if you write it the right way, you just could call Database->getContent($variable);

for better understanding what needs to be in the class here is a simple example of the way it could work:

CODE

...
function doQuery($sql) {
        return mysql_query($sql, $this->mConn);
    }
...

# the function doQuery() is used in the function called Query!

function Query($sql, $fname = FALSE) {

        $this->mLastQuery = $sql;
        
        # Add a comment for easy SHOW PROCESSLIST interpretation
        if ($fname) {
            $commentedSql = "/* $fname */ $sql";
        } else {
            $commentedSql = $sql;
        }
        
        # Do the query and handle errors
        $result = $this->doQuery($commentedSql);

        # Try reconnecting if the connection was lost
        if (FALSE === $result && ($this->lastErrno() == 2013 || $this->lastErrno() == 2006) ) {
            if ($this->Ping() ) {
                $result = $this->doQuery($commentedSql);
            } else {
                $this->mError = 'MySQL Failure: <b>Connection was lost and reconnecting to the MySQL Server Failed</b>';
                return FALSE;
            }
        }

        if (FALSE === $result) {
            $this->mError = '<b>Fatal Error:</b> Could not <b>' . "/* ". $fname . " */" . '</b> '.$this->lastErrno().': <br /><br /> '.$this->lastError();
            return FALSE;
        }
        return $result;
    }


Well, if someone would want all the Class Database posted, PM or Email something like that, but you will need to know PHP to edit it and to understand how it works. wink.gif
Go to the top of the page
 
+Quote Post
bakr_2k5
post Dec 3 2006, 07:40 PM
Post #9


Member - Active Contributor
Group Icon

Group: Members
Posts: 83
Joined: 25-September 06
From: The Netherlands
Member No.: 16,153



QUOTE(Hercco @ Nov 23 2006, 05:44 PM) *

Bakr_2k5, NULL is a bit different in databases and programming languages like PHP.

NULL in PHP, like in C means a pointer to "nothing" while NULL is databases basically is for indicating "no value set". It's also improtant to bear in mind that NULL is databases does not equal to emptry string.

So when you test
CODE

if ($mysql_result == NULL )


you are essentially testing if the handle to the query result is NULL. And if the query is performed and the result is returned to that variable it is never NULL regardless of the query result.

Database can return a NULL for a value of a field.


Ok, didn't know that! rolleyes.gif

But what about this?
CODE
<?PHP
$query = "SELECT * FROM non_existent";
$menu_result = mysql_query($query) or NULL;

if($menu_result == NULL) {
    echo "Yes";
} else {
    echo "No";
}
?>

Here "or" is a PHP function if I'm right (right?). So it should pass the PHP like NULL to $menu_result. And not the "no value set" thing. So $menu_result = NULL ..
And it's the same thing as:
CODE
<?PHP
$var = NULL;
if(is_null($var)) {
    echo "Yes";
} else {
    echo "No";
}
?>

Which results in "Yes". So I don't really understand it! blink.gif

Maybe it's something i misunderstand about the "or NULL" thing. Well my point of view seems logical but for PHP itself it isn't logical mellow.gif!

Bakr_2k5

This post has been edited by bakr_2k5: Dec 4 2006, 05:36 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL & PHP coding(9)
  2. Php/mysql Data Display(3)
  3. Need Help With A PHP - MySQL Registration Script(13)
  4. Need For PHP/MySQL Creator(1)
  5. Printing Out A Table(6)
  6. Need Some Help Using PHP & MySQL(4)
  7. [PHP + MySQL] Separating The Results By Pages(0)
  8. [PHP + MySQL] Encrypting Data(11)
  9. [php] Index.php?section=xx&pag=yy(6)
  10. How Do You Create A Secure Loging?(4)
  11. Important: Basics Of Using PHP And MySQL(10)
  12. Need Help With Php/mysql And Web Servers Such As Asta's.(4)
  13. Need An Alternative To $http_post_data For PHP4(5)
  14. Re-order MySQL Table(11)
  15. PHP & MySQL: Displaying Content From A Given ID(6)
  1. How To Show Serial Nums In PHP Table For Contents Of MySQL DB(4)
  2. Php Mysql Errors(2)
  3. Sql Injection Prevention (passing Numerical Data Across Pages).(9)
  4. Php/mysql And Manual Page Caching?(4)
  5. Too Many Connections?(4)
  6. Extracting Mysql Maths Using Php(2)
  7. Anyone Know Of A Really Good Mysql Class?(4)
  8. Warning: Mysql_num_rows()(1)
  9. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  10. Making A Link = Mysql_query(8)
  11. Making Something In Mysql Happen Only Once(10)
  12. Mysql Question(inserting Number From A Textfield)(3)
  13. Letting Users Add Mysql Data With Php(1)


 



- Lo-Fi Version Time is now: 5th December 2008 - 01:39 AM