wsantos Posted November 7, 2007 Share Posted November 7, 2007 Just need tips on error traps and handling. For this function what is the BEST way to handle null result and etc. function query_execute($strConn,$strQuery) { $result = mysql_query($strQuery,$strConn); if(!$result) { $message = 'Invalid query: ' . mysql_error() . '\n' . 'Whole query: ' . $$ die($message); } while($data = mysql_fetch_array($result, MYSQL_ASSOC)) $dataarr[] = $data; return $dataarr; } Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 7, 2007 Share Posted November 7, 2007 You mean if there are zero rows returned, IE: no data? Change it to this function instead :-o function query_execute($strConn,$strQuery) { $result = mysql_query($strQuery,$strConn) or die("Invalid query: ".mysql_error()); if(!mysql_num_rows($result)) { die("No results were returned, try a different query."); } while($data = mysql_fetch_array($result, MYSQL_ASSOC)) $dataarr[] = $data; return $dataarr; } Add an or die() at the end, never show your exact query, the mysql_error() should give you enough information to determine what went wrong. If there were 0 rows returned, it will die the No results returned error, otherwise it continues on... Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 7, 2007 Share Posted November 7, 2007 why are you even doing that? mysql provides a powerful error tool, and you are only slowing down a processes Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 7, 2007 Author Share Posted November 7, 2007 why are you even doing that? mysql provides a powerful error tool, and you are only slowing down a processes I'm not aware of such error tool. Can you elaborate on this one? Im trying to use this function as part of my pet project. I tried a multiple query and store them on different arrays since joins provide to much pegging on our system. Now I have a trap on the main file but I also want it on my functions and subroutines for flexibility on further projects. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 7, 2007 Share Posted November 7, 2007 well you should always have queries as <?php $q = "Select some stuff from `a table` where This = that"; $r = mysql_query($q) or die(mysql_error()); //If a select and you want to run a array fetch if(mysql_num_rows($r)){ while($row = mysql_fetch_array($r)){ } } else{ //No results } ?> That is the general idea the or die(mysql_error()); is the accepted "proper" way to query and display errors. suppressing errors will get you no help, and should rarely if not never be done. as for your function your are defining stuff that doesn't always need to be defined as you might be simplying using the output in the while($row = fetch) part directly to an output. In your case you are only complicating things. Also just a note the connection is not needed, only if you are connected to multiple DBs which is also a rare case. Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 7, 2007 Author Share Posted November 7, 2007 This is why I need it as a function. $qryCDR = "SELECT DISTINCT so on and so forth"; $qryCP = "SELECT DISTINCT so on and so forth"; $qryCID = "SELECT DISTINCT so on and so forth"; // Open first connection prior to this section $daCID = query_execute($conFL,$qryCID); $daCDR = query_execute($conFL,$qryCDR); // Closed first connection after this section // Open second connection after this section $daCP = query_execute($conNJ,$qryCP); // Closed second connection after this section It is not only a single db but multiple cb from multiple connection. Thanks for the tip on the die. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 7, 2007 Share Posted November 7, 2007 but you don't you are doing a function in a function for no reason only making it worse as you aren't using an sql error. if you have mulitple connections then include them, but just use the or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
wsantos Posted November 7, 2007 Author Share Posted November 7, 2007 yes I already combined your tip with krats. function query_execute($strConn,$strQuery) { $result = mysql_query($strQuery,$strConn) or die("Invalid query: ".mysql_error()); if(mysql_num_rows($result)) { while($data = mysql_fetch_array($result, MYSQL_ASSOC)) $dataarr[] = $data; } return $dataarr; } However, I'm back to the point where I must figure out how I will handle no result. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 7, 2007 Share Posted November 7, 2007 Missed a few things and this does that, and i added a thing to get the keys back too <?php function query_execute($strConn,$strQuery) { $result = mysql_query($strQuery,$strConn) or die("Invalid query: ".mysql_error()>"<br />".$strQuery); if(mysql_num_rows($result)){ $datarr = array(); while($data = mysql_fetch_array($result, MYSQL_ASSOC)){ foreach($data as $key => $value){ $dataarr[$key][] = $value; } } } else{ $dataarr = "no results"; } return $dataarr; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.