2017-08-06 150 views
-1

無論我試圖修復它們多少,我都會遇到這些問題。 我正在處理Html可視化編輯器CMS,每次我登錄這些錯誤顯示。 希望這篇文章清晰易懂。如果不問我,我會盡我所能來回答你。 下面是錯誤的:

call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object 
Call to a member function execute() on boolean 

下面的代碼:提前

class database{ 
    public $root_db = 'cms'; 
    public $server_name = 'localhost:3306'; 
    public $root_db_user = 'root'; 
    public $root_db_pass = ''; 
    public $connection; 
    public $error = false; 

    function connect_to_db(){ 
     $this->connection = new mysqli($this->server_name,$this->root_db_user,$this->root_db_pass,$this->root_db); 

     if($this->connection->connect_error){ 
      $this->error = "Connection Failed: " . $this->connection->connect_error; 
     } 
    } 

    function executeCleanQuery($query,$params){ 
     $type_string = ''; 
     $param_arr = array(); 

     if(!empty($params)){ 
      for($i = 0; $i < count($params); $i++){ 
       $type = gettype($params[$i]); 
       $type_string .= $type[0]; 

       $param_arr[$i] = $this->connection->real_escape_string(htmlentities($params[$i])); 
      } 

      array_unshift($param_arr,$type_string); 

      if(!$stmt = $this->connection->prepare($query)) 
       $error = "Unable to prepare statement"; 

      call_user_func_array(array($stmt,"bind_param") ,$this->refValues($param_arr)); 

      if (!$stmt->execute()) 
       $error = "Unable to execute statement"; 

      $result = $stmt->get_result(); 

      if($result && (strstr($query,"SELECT") || strstr($query,"select"))) 
       return $result->fetch_all(MYSQLI_ASSOC); 
      else{ 
       return $this->connection->insert_id; 
      } 
     }else if(strstr($query,"SELECT") || strstr($query,"select")){ 
      $result = $this->connection->query($query); 
      return $result->fetch_all(MYSQLI_ASSOC); 
     }else{ 
      return true; 
     } 
    } 

    function refValues($arr){ 
     if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+ 
     { 
      $refs = array(); 
      foreach($arr as $key => $value) 
       $refs[$key] = &$arr[$key]; 
      return $refs; 
     } 
     return $arr; 
} 
} 

和感謝。

+1

'對不起,長頭銜哈哈。我討厭告訴你,但那個完全可怕的頭銜會殺死你的問題。它要麼被低估並關閉(並最終被刪除),否則將被忽略。努力寫出一個有意義的標題來真正解釋你的問題,或者期望遇到問題。這次你可能會得到一個答案,但它會在未來的事件中給你帶來問題 – psubsee2003

+0

對此很抱歉。 –

回答

1

錯誤消息很明顯。您應該學會閱讀錯誤消息,處理錯誤消息,並根據錯誤消息採取行動。

call_user_func_array()預計參數1是一個有效的回調,第一陣列構件是不是有效的類名或布爾

的第一部分對象 呼叫一個成員函數執行()錯誤_call_user_func_array()_確切地告訴你在哪裏找到錯誤。鑑於你的代碼,它只能是上線如下所示(儘管錯誤消息還包括行號,所以使用的行號將是有用的):

if(!$stmt = $this->connection->prepare($query)) 
    $error = "Unable to prepare statement"; 

// this is the only "call_user_func_array" in your code, so must be the issue 
call_user_func_array(array($stmt,"bind_param"), $this->refValues($param_arr)); 

錯誤的下一個部分 - 第一個數組成員不是有效的類名稱或對象。調用布爾型上的成員函數execute()會告訴您,具體來說,array($stmt, "bind_param")不是有效函數,因爲$stmt是布爾值。

投擲的原因是因爲雖然您檢查是否設置了$stmt,但如果它不是,您實際上不會阻止繼續操作

雖然「創可貼」的解決方案來演示如何處理它(一個強大的解決方案將包括更好的錯誤處理),就可以解決這個問題,如果你改變你的代碼如下圖所示:

if(! $stmt = $this->connection->prepare($query)) 
    $error = "Unable to prepare statement"; 
    // stop here, $stmt is not set, cannot continue 
    die(); 
} 

call_user_func_array(array($stmt,"bind_param"), $this->refValues($param_arr)); 

最後, 爲什麼這個錯誤正在發生 - 幾乎肯定$query中的值不是一個有效的查詢,並且是罪魁禍首。

+0

謝謝,解決了我的錯誤。 –

+0

一定要檢查我最後一句話:真正的問題是'$ query'無效。我會鼓勵你弄清楚爲什麼/如何發生,並防止_that_ –

相關問題