2015-01-07 88 views
-2

我有一點PHP代碼可以在我的生產服務器上正常工作,但不在我的測試服務器上。下面是代碼:測試服務器上的PHP錯誤

function callProcedure0(&$connection, $procname, $dofunction) 
{ 
    mysqli_multi_query($connection, "CALL " . $procname . "();"); 
    $first_result = 1; 
    do 
    { 
     $query_result = mysqli_store_result($connection); 
     if ($first_result) 
     { 
      $dofunction($query_result); 
      $first_result = 0; 
     } 
     if ($query_result) 
     { 
      mysqli_free_result($query_result); 
     } 
     $query_result = NULL; 
    } while(mysqli_next_result($connection)); 
} 

...

function doGenres($in_result) 
{ 
    global $genre_array, $game_array, $genre_order_array; 
    $genre_count = 1; 
    // foreach is necessary when retrieving values since gaps may appear in the primary key 
    while ($genre_row = mysqli_fetch_row($in_result))   // line 81 is here! 
    { 
     $genre_array[] = $genre_row[0]; 
     $genre_order_array[$genre_row[1] - 1] = $genre_count; 
     $game_array[] = [[],[]]; 
     $genre_count += 1; 
    } 
} 

...

callProcedure0($con, "get_genres_front", doGenres);   // line 138 is here! 

的 「get_genres_front」 位指的是在我的數據庫中的存儲過程。下面是錯誤的:

Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138 

同樣,也有在生產服務器上沒有問題,這是使用Apache 2.2.23,MySQL的73年5月1日-CLL,PHP 5.4.26。事情破裂的測試服務器運行Apache 2.4.10,MySQL 5.6.21,PHP 5.5.19。

在最近的軟件版本中做了些改變嗎?謝謝。

這不是一個重複的問題。我擔心第一個錯誤。我已經知道如何解決我刪除的第二個錯誤。

回答

2

您發佈的代碼是錯誤的,您必須將函數名稱作爲字符串傳遞,然後使用call_user_func來調用此函數。

在你callProcedure0功能變化

$dofunction($query_result); 

call_user_func($dofunction, $query_result); 

然後用函數名稱稱呼其爲這樣

callProcedure0($con, "get_genres_front", "doGenres"); 

上面的代碼可以用也行字符串用

調用該函數
$dofunction($query_result); 

在一些PHP版本中,但傳遞函數名稱的行應該是字符串,否則PHP會假定它是一個常量。

+0

將查詢結果作爲數組轉換不是必需的,否則您的解決方案就可以工作。 – posfan12