Hello,

I have a piece of code I am trying to resolve and I believe it has something to do with using a newer version of PHP or MySQL than the code was developed on. The function that this code is doing works fine, I just get the error message. Here is the error and the code:

GENERAL INFO
Platform: Windows 2003
PHP Version 4.4.4
MySQL Version: 5.2.0 (Windows) running on same machine as PHP script.

ERROR MESSAGE
Notice: Undefined variable: counter in c:\inetpub\wwwroot\assets\servers.php on line 95

CODE - I typed the line numbers to the left for reference
94: while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
95: if($counter++ % 2 == 0){
96: $td = "even";
97: }else{
98: $td = "odd";
99: }
100: echo"<tr>
101: <td class='$td'><a href='editserver.php?id=$row[id]'>Edit</a></td>

    Error reporting level is set to so that it shows also notices(propably set to E_ALL). Of course it would be great to correct those at the code level. You should always define your variables. In this case, give the $counter variable a value before raising its value with $counter++

    $counter = 0; // this defines $counter's base value
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if($counter++ % 2 == 0){
    ...
    

    If you have too much of those maybe it should be wise to lower the error reporing level. You can do that in php.ini or in the www-servers configfile(for example if its apache, httpd.conf).

    Anyway if you dont want those notices you can set error_reporting to for example E_ALL & ~E_NOTICE which gives normal errors without notices.

      cahva,

      Thank you very much for the help. I gave the $counter a value as you suggested and it worked. It's easy to overlook the simple stuff 🙂

      Thank you again!!

        Write a Reply...