2010-10-18 54 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