0

I've built a web app that works perfectly in my development environment. In my production environment, however, I get notices that I cannot reproduce in my dev environment although I've set PHP to report all errors with 'error_reporting(E_ALL);' at the start of the page. eg,'Notice: Undefined index: name in /home/bontshit/public_html/admin/library/functions.php on line 202'. How can I track down the problem without simply muting the error reporting?

I'm using The AppServ Open Project - 8.6.0 for Windows Now you running on PHP 5.6.30; my production environment is on the same version of PHP. I've added the code the notice I mentioned refers to and clearly (I believe) the index 'name' was defined.

$categories = array();
while($row = dbFetchAssoc($result)) {
    extract($row);

    if ($cat_parent_id == 0) {
        // we create a new array for each top level categories
        $categories[$cat_id] = array('name' => $cat_name, 'children' => array());
    } else {
        // the child categories are put into the parent category's array
        $categories[$cat_parent_id]['children'][] = array('id' => $cat_id, 'name' => $cat_name);    
    }
}   

// build combo box options
$list = '';
foreach ($categories as $key => $value) {
    $name     = $value['name'];
    $children = $value['children'];

    $list .= "<option value=\"$key\"";

    if ($key == $catId) {
        $list.= " selected";
    }

    $list .= ">$name</option>\r\n";

    foreach ($children as $child) {
        $list .= "<option value=\"".$child['id']."\"";
        if ($child['id'] == $catId) {
            $list.= " selected";
        }

        $list .= ">&nbsp;&nbsp;".$child['name']."</option>\r\n";
    }
}
Thaps
  • 131
  • 1
  • 8
  • BTW, "...I get notices that I cannot reproduce..." all the notices are about an undefined index that I actually have defined. As I stated, my dev enviro throws no errors. – Thaps Jun 09 '19 at 23:03
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Progman Jun 09 '19 at 23:03
  • You can switch them off with `\error_reporting(\E_ALL & ~\E_NOTICE);`, but they can be very helpful to you in Development and you should switch off `\ini_set('display_errors', 0);` in production anyway. – Dharman Jun 09 '19 at 23:05
  • @Dharman, thanks. I don't want to sit on errors. Especially errors I can't explain to myself like this one. – Thaps Jun 09 '19 at 23:07
  • BTW using [`extract()`](https://www.php.net/manual/en/function.extract.php) is frowned upon, are you sure there is no other way? – Dharman Jun 09 '19 at 23:08
  • See [Error reporting basics](https://phpdelusions.net/articles/error_reporting) – Dharman Jun 09 '19 at 23:16
  • 1
    @Dharman, a quick google on 'extract()' as per your comment lead me here: https://stackoverflow.com/questions/829407/what-is-so-wrong-with-extract... I've personally found the function convenient because I can quickly work with the variable names and if they where wisely chosen to begin with then no confusion arises for me. I don't think it's the problem because then I would see it on my dev enviro too- no? – Thaps Jun 09 '19 at 23:17
  • 1
    The article you mention is correct. I merely wanted to direct your attention to a bad programming practice, I did not say it was the cause of your notices. If it makes your life easier go ahead, but beware of the red warning from the PHP manual. – Dharman Jun 09 '19 at 23:20
  • @Dharman, great food for thought. I agree that the production user should never see errors but my approach has been to make things that work properly, not hide the error. The security aspects are what I hadn't thought of because I always try validate inputs from the user side; yet maybe your right about not showing any errors at all. – Thaps Jun 09 '19 at 23:26
  • If the `$cat_parent_id` is not zero your code doesn't initialise `$categories[$cat_parent_id]['name']`, so it's undefined later on. –  Jun 09 '19 at 23:46
  • @ReddHerring, thanks for the comment.. $cat_parent_id is tested but if it is not zero then the 'else' block takes care of initializing $categories. Also, in terms of my troubles, why no errors in the dev enviro? – Thaps Jun 10 '19 at 00:02
  • @Progman, thanks for your input. https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined is interesting but not the same because the problems described there would appear on my dev side too- no? – Thaps Jun 10 '19 at 00:04
  • It may be worth saying that I've used the snippet of code I put up for years without incident. I just can't put a finger on what may have changed with PHP in 2019. Or is it that I'm using Appserv? – Thaps Jun 10 '19 at 00:08
  • Maybe it's just dirty data.. along the lines of Dharman's thinking.. I'm gonna try reload the data itself and then let you guys know. Thank you all. – Thaps Jun 10 '19 at 00:15

1 Answers1

0

I simply flushed out the contents of my db and reloading the data. So far everything is working again. I don't know how it corrupted to begin with but I'm glad to move forward. Wow! After all that. KISS principle at its best :P .. Thanks guys.

Thaps
  • 131
  • 1
  • 8