augrunt Posted January 19, 2010 Share Posted January 19, 2010 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/ Share on other sites More sharing options...
o3d Posted January 19, 2010 Share Posted January 19, 2010 try to put a try-catch over your mysql_query function or when the error happens, error_log the query so you can see if the problem exist with the query or not. Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997806 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 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()); Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997816 Share on other sites More sharing options...
augrunt Posted January 19, 2010 Author Share Posted January 19, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997821 Share on other sites More sharing options...
oni-kun Posted January 19, 2010 Share Posted January 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997822 Share on other sites More sharing options...
augrunt Posted January 19, 2010 Author Share Posted January 19, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997827 Share on other sites More sharing options...
augrunt Posted January 19, 2010 Author Share Posted January 19, 2010 //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 ([email protected]) 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? Quote Link to comment https://forums.phpfreaks.com/topic/188979-error-handling-a-certain-function-phpmysql/#findComment-997837 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.