Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question How to Stop MySQL Errors?

    hello,
    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!
    -Ken

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    place an @ sign in front of your query
    i.e $result = @mysql_query('SELECT * WHERE 1=1');

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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() 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:

    PHP Code:
    if( isset($myvar) )
    {
      echo 
    'variable is set';
    }
    else
    {
      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.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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!
    -Ken

  • #5
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would do something like:

    PHP Code:
    if( isset($_REQUEST['view']) )
    {
      
    $view $_REQUEST['view'];
    }
    else
    {
      
    $view NULL;

    Then, when you need to use $view, you can run something like:

    PHP Code:
    if( !is_null($view) )
    {
      
    //$view has been passed from your query string, so you can use it normally
    }
    else
    {
      
    //$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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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...

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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!


    Thanks!
    -Ken

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:
    $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:
    */
         
    'GLOBALS',
         
    '_GET',
         
    '_POST',
         
    '_COOKIE',
         
    '_REQUEST',
         
    '_SERVER',
         
    '_ENV',
         
    '_FILES',
         
    'passthrough'
    );

    // Here we go!
    if (is_array($GLOBALS))
    {
        foreach (
    $GLOBALS AS $ar_keys => $ar_vals)
        {
            if (!
    in_array($ar_keys$passthrough) AND $ar_keys != 'ar_keys' AND $ar_keys != 'ar_vals')
            {
                unset(
    $GLOBALS["$ar_keys"]);
            }
        }
    }
    else
    {
        
    $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.

    Edit:
    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
    Last edited by Fou-Lu; 07-11-2005 at 08:04 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •