开发者

PHP MySQL script gone wrong

开发者 https://www.devze.com 2023-01-20 20:12 出处:网络
I\'m working on a site and created this experimental script which populates a category menu dynamically based on the database entry.

I'm working on a site and created this experimental script which populates a category menu dynamically based on the database entry.

It worked for a day and then suddenly stopped. I changed my includes for requires and it gave me this error message

Fatal error: Maximum execution time of 30 seconds exceeded in /home1/advertbo/public_html/dev_area/origocloud/include/views/blog/dbget.php on line 34

function getBlogMenu(){
$dbhost = 'localhost';
$dbuser = ' ';
$dbpass = ' ';

$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ado_ocblog", $con);

$htmlString = "";

$result = mysql_query(
    "SELECT *
    FROM subCat
    JOIN headCat ON subCat.headid = headCat.id
    ORDER BY headid ASC;");

$array = mysql_fetch_array($result);
mysql_close($con);

$pre = NULL;
$hc = 0;
$sc = 1;
while ($array) {
    if($pre == NULL){
        $pre = $row["headc"];
        $test[0][0]=$row["headc"];
        $test[0][1]=$row["subc"];

    }
    else
    {
        if($pre ==$row["headc"]){
            $sc++;
            $test[$hc][$sc] = $row["subc"];

        }
        else
        {
            $hc++;
            $sc = 1;
            $test[$hc][0]=$row["headc"];
        开发者_如何学C    $test[$hc][$sc]=$row["subc"];
            $pre = $row["headc"];
        }
    }

}

foreach( $test as $arrays=>$cat)
{
        $first = TRUE;
        foreach($cat as $element)
        {
            if($first == TRUE)
            {
                $htmlString.= '<h3><a href="">'.$element.'</a></h3>
                        <div>
                            <ul>
                    ';
                $first = FALSE;
            }
            else
            {
                $htmlString.= '<li><a class="sub_menu" href="#">'.$element.'</a></li>';
            }

        }
        $htmlString.= '</ul> </div>';
}
return $htmlString;


}

I'm really stuck, the page just keeps timing out the point where i call the function


Try this:

while ($array = mysql_fetch_array($result)) {}

Take a look on PHP docs http://php.net/mysql_fetch_array

If does not work, your SQL Query returns too much values and craches the php execution

=]


I think it's time to take a step back and look at what you're doing :) This function should do what you want (even if you fixed the infinite loop problem in the function you gave, I don't think it would act how you want it to.):

function getBlogMenu(){
  $dbhost = 'localhost';
  $dbuser = ' ';
  $dbpass = ' ';

  $con = mysql_connect($dbhost, $dbuser, $dbpass);
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("ado_ocblog", $con);

  $htmlString = "";
  $result = mysql_query(
      "SELECT *
      FROM subCat
      JOIN headCat ON subCat.headid = headCat.id
      ORDER BY headid ASC;");

  // arrays can have strings as keys as well as numbers,
  // and setting $some_array[] = 'value'; (note the empty brackets []) 
  // automatically appends 'value' to the end of $some_array,
  // so you don't have to keep track of or increment indexes
  while ($row = mysql_fetch_assoc($result))
  {
    $test[$row["headc"]][] = $row["subc"];
  }

  // don't close the connection until after we're done reading the rows
  mysql_close($con);

  // $test looks like: array('headc1' => array('subc1', 'subc2', 'sub3'), 'headc2' => array('subc4', 'subc5'), ...)
  // so we step through each headc, and within that loop, step through each headc's array of subc's.
  foreach($test as $headc => $subc_array)
  {
    $htmlString.= '<h3><a href="">'.$headc.'</a></h3><div><ul>';
    foreach($subc_array as $subc)
    {
      $htmlString.= '<li><a class="sub_menu" href="#">'.$subc.'</a></li>';
    }
    $htmlString.= '</ul></div>';
  }

  return $htmlString;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号