PHP MySQL脚本出错了

Art*_*ski 1 php mysql timeout fatal-error

我正在一个站点上创建这个实验脚本,该脚本根据数据库条目动态填充类别菜单.

它工作了一天然后突然停了下来.我更改了我的包含需求,它给了我这个错误信息

致命错误:第34行/home1/advertbo/public_html/dev_area/origocloud/include/views/blog/dbget.php超过30秒的最长执行时间

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"];
            $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;


}
Run Code Online (Sandbox Code Playgroud)

我真的卡住了,页面只是保持超时我调用函数的点

Gad*_*ski 6

试试这个:

while ($array = mysql_fetch_array($result)) {}
Run Code Online (Sandbox Code Playgroud)

看看PHP文档http://php.net/mysql_fetch_array

如果不起作用,您的SQL查询会返回太多值并且会破坏php执行

=]

  • 安德烈是对的.你正在从while()表达式中获取赋值`$ array = mysql_fetch_array($ result)`.你不能这样做,否则$ array永远不会改变,你永远不会离开while循环. (2认同)