|
|
|
|
![]() ![]() |
Nov 21 2006, 04:46 PM
Post
#1
|
|
|
Member - Active Contributor Group: Members Posts: 83 Joined: 25-September 06 From: The Netherlands Member No.: 16,153 |
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: 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! Bakr_2k5 This post has been edited by bakr_2k5: Nov 21 2006, 05:40 PM |
|
|
|
Nov 21 2006, 05:51 PM
Post
#2
|
|
|
Super Member Group: [HOSTED] Posts: 500 Joined: 5-November 06 Member No.: 17,016 myCENTs:NEGATIVE[-20.12] |
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: 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! 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 |
|
|
|
Nov 21 2006, 05:57 PM
Post
#3
|
|
|
Member - Active Contributor Group: Members Posts: 83 Joined: 25-September 06 From: The Netherlands Member No.: 16,153 |
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 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 This post has been edited by bakr_2k5: Nov 21 2006, 06:02 PM |
|
|
|
Nov 21 2006, 06:29 PM
Post
#4
|
|
|
Super Member 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 |
|
|
|
Nov 21 2006, 06:48 PM
Post
#5
|
|
|
Advanced Member 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. |
|
|
|
Nov 21 2006, 07:41 PM
Post
#6
|
|
|
Member - Active Contributor Group: Members Posts: 83 Joined: 25-September 06 From: The Netherlands Member No.: 16,153 |
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 |
|
|
|
Nov 23 2006, 05:44 PM
Post
#7
|
|
|
Super Member 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 |
|
|
|
Nov 23 2006, 07:06 PM
Post
#8
|
|
|
the Q 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. |
|
|
|
Dec 3 2006, 07:40 PM
Post
#9
|
|
|
Member - Active Contributor Group: Members Posts: 83 Joined: 25-September 06 From: The Netherlands Member No.: 16,153 |
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! 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! 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 This post has been edited by bakr_2k5: Dec 4 2006, 05:36 PM |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th December 2008 - 01:39 AM |