Jump to content

Error Handling a certain function -- PHP/MYSQL


augrunt

Recommended Posts

Hey guys,

 

I need a little bit of help. I have done well so far with my little project and has progressed to such an extent that it is now used by over 8000 users monthly with over 2000 hits daily but I need some help.

 

Lately I have been getting hits in my error log every now and then with

[18-Jan-2010 19:47:35] PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/####/public_html/####/#####/lib.php on line 34

 

Now I know what that means, the query was either empty or incomplete... but all of my code relies on that query, so it's very inconsistent. I have over 700 lines alone in my Lib.php file and adding up all the files that use it, probably 2000 lines all up. I need to identify "where" it's being included and what variables are being passed (and in this case, not being passed) so that the query fails.

 

I have investigated "set_error_handler()" but that will handle all errors and not "die" when caught according to php.net and I just want it to handle the "mysql" error and log it with the details I have requested ($_SERVER['REQUEST_URI'] & Variables) so I can troubleshoot it better.

 

Does anyone know how I can go about this? or if you can figure out why my array might be failing, that'd be good. Because the only time I pass thru anything is when I have $check_id set... and that's handled by an API, so I can trust it 100%...

 

Here is the function that is the center of it all... [lib.php]

 

function retrieve_data($check_id) {
if ($check_id == NULL) { die('No ID Submitted.'); }
get_db_conn();
global $first_time;
$result = mysql_query("SELECT * FROM users where user_id='$check_id'");
$row = mysql_fetch_array( $result );

Link to comment
Share on other sites

The problem is with the query as it is not returning a valid resource..

If you change your query call to this it will tell you what is wrong..

$result = mysql_query("SELECT * FROM users where user_id='$check_id'") or trigger_error(mysql_error());

Link to comment
Share on other sites

The problem is with the query as it is not returning a valid resource..

If you change your query call to this it will tell you what is wrong..

$result = mysql_query("SELECT * FROM users where user_id='$check_id'") or trigger_error(mysql_error());

 

Will that log all the other details as well? like the page that used the function? I don't trigger the error myself. This is the problem, I don't know what is actually causing this error because I have went through my whole script with as many possibilities as possible, such as; Logged in, Logged out, Unauthorized, etc. and I can't trigger it for the life of me. So I need to figure out "why". I know that the query is not returning a valid resource. The error tells me that in clear PHP-muffled English! ;)

 

 

Link to comment
Share on other sites

The problem is with the query as it is not returning a valid resource..

If you change your query call to this it will tell you what is wrong..

$result = mysql_query("SELECT * FROM users where user_id='$check_id'") or trigger_error(mysql_error());

 

Will that log all the other details as well? like the page that used the function? I don't trigger the error myself. This is the problem, I don't know what is actually causing this error because I have went through my whole script with as many possibilities as possible, such as; Logged in, Logged out, Unauthorized, etc. and I can't trigger it for the life of me. So I need to figure out "why". I know that the query is not returning a valid resource. The error tells me that in clear PHP-muffled English! ;)

 

This will trigger an error such as: "Fatal Error: MySQL query returns resource ID #4 on line xxx".. You can log the errors using an error log, which you can define in php.ini.

Link to comment
Share on other sites

The problem is with the query as it is not returning a valid resource..

If you change your query call to this it will tell you what is wrong..

$result = mysql_query("SELECT * FROM users where user_id='$check_id'") or trigger_error(mysql_error());

 

Will that log all the other details as well? like the page that used the function? I don't trigger the error myself. This is the problem, I don't know what is actually causing this error because I have went through my whole script with as many possibilities as possible, such as; Logged in, Logged out, Unauthorized, etc. and I can't trigger it for the life of me. So I need to figure out "why". I know that the query is not returning a valid resource. The error tells me that in clear PHP-muffled English! ;)

 

This will trigger an error such as: "Fatal Error: MySQL query returns resource ID #4 on line xxx".. You can log the errors using an error log, which you can define in php.ini.

 

Yes, I understand that. However it returns the error on the LIB.PHP file not the other 15 files that access the function! I suppose the try{ and } catch {} will have to do since I do not wish to modify the error_log that has been set by PHP.ini. I don't get the error and my users are not going to send me an email no matter how much I beg them to do so once they receive an error.

 

I am still kinda balancing between creating a customized error_handler that will keep the users accessing the pages out of the loop (don't show them sensitive error information) and log the error as normal and keep everything running smoothly, anyone have a template they can share?

Link to comment
Share on other sites

//Find & log this f#*$ing blasted error!
function error($message, $level= 0 ) {
    $caller = next(debug_backtrace());
    error_log($message.' in <strong>'.$caller['function'].'</strong> called from <strong>'.$caller['file'].'</strong> on line <strong>'.$caller['line'].'</strong>'."\n<br />error handler", $level);
    die('This app has encountered an error, Please inform the developer about the issue so that it may be corrected.');
}

function retrieve_data($check_id) {
        if ($check_id == NULL) { die('No ID Submitted. Please notify the me via (augrunt@hotmail.com) about what page you were on and what you tried to do before you saw this error as I am trying to find out what is wrong. Thank you'); }
        
        get_db_conn();
        global $first_time;
        $result = mysql_query("SELECT * FROM users where user_id='$check_id'") or error(mysql_error());

 

How's this now?

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.