bakr_2k5 0 Report post Posted November 21, 2006 (edited) Hello again 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: $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! Bakr_2k5 Edited November 21, 2006 by bakr_2k5 (see edit history) Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted November 21, 2006 (edited) Hello again 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: $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! Bakr_2k5 Here, just tested it, and works. I'm using php5 and mysql5 on winxp pro<?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 Edited November 21, 2006 by faulty.lee (see edit history) Share this post Link to post Share on other sites
bakr_2k5 0 Report post Posted November 21, 2006 (edited) Yeah awesome, it works ;)Thank you faulty.lee!Well could think of it myself now I see it, but my mind isn't very clear today :)Anyways thank you! And problem solved :DBakr_2k5EDIT:is_null($menus_result) works here! Using the latest XAMPP version on linux (Gentoo).EDIT2:is_null($menus_result) doesn't work ... it gives always a FALSE! Strange Edited November 21, 2006 by bakr_2k5 (see edit history) Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted November 21, 2006 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 Share this post Link to post Share on other sites
iGuest 3 Report post Posted November 21, 2006 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. Share this post Link to post Share on other sites
bakr_2k5 0 Report post Posted November 21, 2006 (edited) 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!$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.http://php.net/is_nullis_null -- Finds whether a variable is NULLThis 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 Edited November 21, 2006 by bakr_2k5 (see edit history) Share this post Link to post Share on other sites
Hercco 0 Report post Posted November 23, 2006 (edited) 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 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. Edited November 23, 2006 by Hercco (see edit history) Share this post Link to post Share on other sites
Quatrux 4 Report post Posted November 23, 2006 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: ...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. Share this post Link to post Share on other sites
bakr_2k5 0 Report post Posted December 3, 2006 (edited) 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 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! But what about this?<?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:<?PHP$var = NULL;if(is_null($var)) { echo "Yes";} else { echo "No";}?>Which results in "Yes". So I don't really understand it! 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 !Bakr_2k5 Edited December 4, 2006 by bakr_2k5 (see edit history) Share this post Link to post Share on other sites
iGuest 3 Report post Posted July 29, 2009 I know that it is the old topic but maybe this will help to some one. bakr_2k5 wants something like this <?php function spell($strings){echo($strings);} $query = "SELECT * FROM menus ORDER BY id ASC";$menus_result = mysql_query($query) or spell("Error!");while( $menu=mysql_fetch_array($menus_result) ) { echo $menu['name']."<br />";} ?> it works, even for me sorry I cant write it into "tag" CODE -reply by Wicky Share this post Link to post Share on other sites