2010-10-18 53 views
1

我正在一个网站上工作,并创建了这个实验性脚本,它根据数据库条目动态地填充一个类别菜单。PHP MySQL脚本出错了

它工作了一天,然后突然停止。我改变包括要求,它给了我这个错误消息

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


} 

我很坚持,页面只是不断超时在哪里调用函数

+2

while($ array)< - 这里是。不是很聪明的构造。您必须学习一些PHP基础知识,请参阅w3c学校关于如何处理来自mysql数据库的数据的一课。 – 2010-10-18 17:48:09

回答

6

点试试这个:

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

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

如果不工作中,你的SQL查询返回太多的价值观和craches php的执行

=]

+2

安德烈是对的。您正在从while()表达式中分配'$ array = mysql_fetch_array($ result)'。你不能这样做,否则$ array永远不会改变,你永远不会离开while循环。 – 2010-10-18 19:20:23

+1

我认为它应该是while($ row = mysql_fetch_array($ result)){...} - 只是一个简单的外观;没有使用$ array。也许他正在混合一些示例脚本......? – handfix 2010-10-18 19:21:11

+0

我在循环中使用$数组,谢谢你的建议家伙,PHP不是我的强项,我一定会检查你的建议。我应该专注于这个mysql_fetch_array吗?我已经看到了一些关于在这些情况下建议使用mysql_fetch_assoc的stackoverflow的建议值得检查吗? – Arturski 2010-10-18 22:16:19

0

我认为现在是时候后退一步,看看你在做什么:)这个功能应该做你想要什么(即使你在给你的函数中修复了无限循环问题,我不认为它会按照你想要的方式行事。):

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

我nitailly这是分裂成2个PHP文件,我加入他们在一起解决这个问题的努力,以在一块。这个功能在最后还是有效的,但是正如前面提到的那样,它开始崩溃了:) – Arturski 2010-10-18 22:22:34

+0

没错,但是我说的是:在你给出的例子中有一些不太好的编程习惯。我试图帮助你,不仅仅是为了这个问题,而是为了未来:) – catgofire 2010-10-27 16:48:01