View Full Version : How to Stop MySQL Errors?
07-11-2005, 03:15 PM
I'm wondering how i can stop my script from showing errors if there is no data to be pulled in mysql/php.
right now i can these errors if there is no data:
Notice: Undefined variable: photos in C:\Program Files\Apache Group\Apache2\htdocs\pics\index.php on line 135
Warning: array_chunk() expects parameter 1 to be array, null given in C:\Program Files\Apache Group\Apache2\htdocs\pics\index.php on line 135
Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\pics\index.php on line 138
how do i suppress these errors?
thanks in advance for your time!
07-11-2005, 04:23 PM
place an @ sign in front of your query
i.e $result = @mysql_query('SELECT * WHERE 1=1');
07-11-2005, 04:25 PM
Short answer -- You can use the '@' character in front of many functions to suppress error messages served from that function. You can also change the level of errors that return messages (see PHP's error_reporting() (http://us2.php.net/manual/en/function.error-reporting.php) function). Neither of these look like really good options for your problem, though.
The heart of your trouble looks like the undefined variable. Instead of just suppressing the error, you should fix the variable. You can do this in a few ways, and without seeing any of your code, it's difficult to suggest a specific one. In general, you should try to make sure all variables have a value, even if their intended value doesn't exist. So for the variable 'photos', you should have some value to set it to if photos aren't there (I'd probably just use a boolean FALSE).
If you have variables that won't be guaranteed to have a value, you should run it through isset() before trying to use it:
if( isset($myvar) )
echo 'variable is set';
echo 'variable is not set -- do not use this variable';
I'm assuming the other errors are related to this one. Either way, they're important enough errors that you should build your script to avoid them rather than trying to just suppress the messages and move on.
07-11-2005, 05:03 PM
ya, see thats the problem, some of these variables come from the querystring,
which doesn't always exist, like when a user first comes to the page, there are no variables in the querystring yet.
so what would i do for something like this
$view = $_REQUEST['view'];
if there is no data to pick up from the REQUEST function, then i get an error.
thanks again for your time!
07-11-2005, 06:11 PM
I would do something like:
if( isset($_REQUEST['view']) )
$view = $_REQUEST['view'];
$view = NULL;
Then, when you need to use $view, you can run something like:
if( !is_null($view) )
//$view has been passed from your query string, so you can use it normally
//$view was not in the query string, modify your routine accordingly
As a side note, I would use $_GET instead of $_REQUEST. That way you'll be sure $view came from your query string and not somewhere else. But that's a personal thing.
07-11-2005, 07:38 PM
ALWAYS ALWAYS ALWAYS MAKE SURE YOUR VARIABLES ARE COMMING FROM WHERE YOU TELL THEM TOO!!!!
Sorry, can't stress this enough. ALWAYS make sure that your variables are initialzed and usable. Don't ever trust that any variable you are using is being created soley within your scripting, and validate any possible injection technique.
This is why register_globals = bad. I have a quick passthough function I can give you if you want it that will strip your registered variables if by chance they are set to be on. Mighty helpful that one is...
07-11-2005, 07:42 PM
sure, i'd love to take a look at it.
I'm still learning this php stuff so any help i can get would be great!
07-11-2005, 08:01 PM
No problem, I use this myself just in case my server has globals on. Remember though, this will force you to only use supergobals, and whatever else you may have defined:
$passthrough = array(
Contains a list of any values you would like to set as registered. Removes any others not contained within this list. Add accordingly at the bottom, 'passthrough' is required:
// Here we go!
foreach ($GLOBALS AS $ar_keys => $ar_vals)
if (!in_array($ar_keys, $passthrough) AND $ar_keys != 'ar_keys' AND $ar_keys != 'ar_vals')
$GLOBALS['_GET'] = &$_GET;
$GLOBALS['_POST'] = &$_POST;
$GLOBALS['_COOKIE'] = &$_COOKIE;
$GLOBALS['_SERVER'] = &$_SERVER;
$GLOBALS['_ENV'] = &$_ENV;
$GLOBALS['_FILES'] = &$_FILES;
Thats it. Test it with globals on as well as off. Should work n/p for you.
Guess I should explain a bit of what I mean, eh?
For something like this, I add things such as $templategroups, or $permissions for instance into the array. I do this so that they are not stripped when accessed by the pages. The way my scripts will handle the information is required that the array for my requested templates or code be sent prior to the initialization and global functions and includes. If this is not added to the list, $templategroup would become undefined.
Hope that makes sense for you :P
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.