Jump to content

[SOLVED] Tips on Error Traps and Handling


wsantos

Recommended Posts

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;
  }

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.