2

I am trying to setup an application availability report, I have an array of with app names in it and a number. I am going through the list and finding the duration of issues for each one and saving it a corresponding array and then doing a calculation on it. I try to print it out to a table but no dice. I have my test echos and for loops still in there and I get a the app array printed out but inside the while look doesn't execute and I can't pinpoint why. Thanks for the help and I can answer questions about it. I am going to put a month/year form input at the bottom with a submit button that calls this same page back.

<?php
                $applist = array('default1','default2','default3');
                $numofapps = 3;
                $i=0;
                $downtime;
                if($_POST['submitted']==1)
                {
                    $month = $_POST['startmonth'];
                }   
                else {
                    $month = date("m");
                }
                echo "<br />";

                switch ($month){
                    case 01:
                    case 03:
                    case 05:
                    case 07:
                    case 08:
                    case 10:
                    case 12:
                        $totaltime = 31 * 1440;
                        break;
                    case 04:
                    case 06:
                    case 09:
                    case 11:
                        $totaltime = 30 * 1440.0;
                        break;
                    case 02:
                        $year = $_POST['startyear'];
                        if(($year%4)==0)
                        {
                            $totaltime = 29 * 1440.0;
                        }
                        else {
                            $totaltime = 28 * 1440.0;
                        }
                        break;

                }
                echo "<br />";
                while($numofapps > $i)
                {
                    $query="Select `duration` from `issuetrack` WHERE `app`='" . $applist[$i] . "'";
                    $result=mysql_query($query);

                    while($row = mysql_fetch_array($result))
                    {
                        echo $downtime[$i];
                        echo "TEST downtime <br />";
                        echo $row[$i];
                        echo "ETST DURATION <br />";
                        $downtime[$i]+=$row['duration'];
                    }
                    $appavail[$i]=($downtime[$i] / $totaltime) * 100;

                    $i++;                   

                    echo "<br />";
                }
                $j=0;
                echo "<table border ='1'>";
                        echo "<tr><th>Application Affected</th><th>Application Availability</th>";

                        while ($numofapps > $j)
                        {
                            echo "<tr><td>";
                            echo $applist[$i];
                            echo "</td><td>";
                            echo $appavail[$i];
                            echo "</td></tr>";
                        }
                        echo "</table>";



            ?>
4
  • 1
    What have you done to bug check? What is the output supposed to look like? Can you remove code that's not related to the problem area? More info, please. Commented Nov 22, 2011 at 5:52
  • Ok I took at the test code. My while($row = fetch) code isn't working and I know I have data to be returned as I have ran the sql command in the phpmyadmin console Commented Nov 22, 2011 at 6:01
  • 1
    What does echo mysql_error(); print out after your $result = mysql_query($query)? Commented Nov 22, 2011 at 6:04
  • Did you try $count = mysql_num_rows($query); and then echo $count; ? Commented Nov 22, 2011 at 6:45

3 Answers 3

4

Check the $result variable like,

if($result)
 echo "1";
else
 echo mysql_error();

Even if you don't have any data, if your query is right then it prints 1, otherwise it prints error if any.

Also check your mysql_connection to find out you've a connection to db, like this:

mysql_connect('localhost','username','password') or die (mysql_error());
Sign up to request clarification or add additional context in comments.

Comments

1

If your script isn't entering while($row = mysql_fetch_array($result)) then it's because the query didn't return any results. Try printing out your query (echo $query) and run it directly in mysql to see what it gives back.

The variables in the while ($numofapps > $j) loop are indexed by $i but they should be indexed by $j. Also, you need a $j++ there or it will loop until the script execution time limit is reached.

Comments

0

as a first step check the mysql connection

mysql_connect('localhost','username','password') or die (mysql_error());

if there is connection then check the query is returning something by using the following query

 $count=mysql_num_rows($result);
       if($count!=0)
       {
           while($row = mysql_fetch_array($result))
          {
               echo $downtime[$i];
               echo "TEST downtime <br />";
               echo $row[$i];
               echo "ETST DURATION <br />";
               $downtime[$i]+=$row['duration'];
          }
      }
     else
     {
         echo "The query returned empty";
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.