2014-09-21 65 views
1

我有以下功能:bind_param()在非對象錯誤

public function detail($detail, $table, $column, $value) { 
     if(is_array($detail)) { 
      $data = array(); 

      foreach($detail as $key) { 
       $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?"); 
       if(is_numeric($value)) { 
        $stmt->bind_param('i', $value); 
       } else { 
        $stmt->bind_param('s', $value); 
       } 
       $stmt->execute(); 
       $stmt->bind_result($detail); 
       $stmt->fetch(); 
       $data[] = $detail; 
      } 

      return $data; 
     } else { 
      $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?"); 

      if(is_numeric($value)) { 
       $stmt->bind_param('i', $value); 
      } else { 
       $stmt->bind_param('s', $value); 
      } 
      $stmt->execute(); 
      $stmt->bind_result($detail); 
      $stmt->fetch(); 

      return $detail; 
     } 
    } 

此功能運作良好,直到我使用的陣列。使用這個函數的方式是這樣的:$db->detail('username', 'users', 'id', 1)這將返回id爲1的用戶的用戶名(這可以很好地工作)。就像我說的,當我使用數組因此,例如問題開始:

$details = array('username', 'active', 'registered'); 
$details = $db->detail($details, 'users', 'id', 1); 
print_r($details); 

錯誤指向$stmt->bind_param('i', $value);if(is_array())。我已經試過了:bind_param on a non-object的回答,但這並沒有幫助我;我仍然得到同樣的錯誤。 我希望有人知道如何修復我的Fatal error: Call to a member function bind_param() on a non-object錯誤。

在此先感謝。

+0

很可能是因爲'準備()'失敗,這就是爲什麼當你綁定,它沒有工作 – Ghost 2014-09-21 10:06:34

+0

可能是因爲您覆蓋變量$詳細其中失敗準備在下一次迭代 – 2014-09-21 10:26:52

回答

1

嘗試取消設置$stmt變量在循環:

public function detail($detail, $table, $column, $value) { 
    if(is_array($detail)) { 
     $data = array(); 

     foreach($detail as $key) { 
      $stmt = $this->mysqli->prepare("SELECT `$key` FROM `$table` WHERE `$column` = ?"); 
      if(is_numeric($value)) { 
       $stmt->bind_param('i', $value); 
      } else { 
       $stmt->bind_param('s', $value); 
      } 
      $stmt->execute(); 
      $stmt->bind_result($detail); 
      $stmt->fetch(); 
      $data[] = $detail; 
      $stmt = null; 
     } 

     return $data; 
    } else { 
     $stmt = $this->mysqli->prepare("SELECT `$detail` FROM `$table` WHERE `$column` = ?"); 

     if(is_numeric($value)) { 
      $stmt->bind_param('i', $value); 
     } else { 
      $stmt->bind_param('s', $value); 
     } 
     $stmt->execute(); 
     $stmt->bind_result($detail); 
     $stmt->fetch(); 

     return $detail; 
    } 
} 

這應該幫助。

+0

這仍然給我同樣的錯誤 – SuperDJ 2014-09-21 10:23:42

+0

這很奇怪,因爲它在我的服務器上正常工作。你在'foreach'主體的末尾添加了'$ stmt = null;'這行嗎? – Zeusarm 2014-09-21 10:26:10

+0

是的,我確實把它放在foreach循環的末尾 – SuperDJ 2014-09-21 10:27:24

1

我認爲在不使用循環的情況下準備查詢的有效方法就是在數組中使用implode值,而不是循環和準備查詢語句。 對於例如: - 如果查詢是

SELECT `username`,`active`,`register` FROM users WHERE ID = 1 //username,active and register can be used in single prepare statement by imploding the array 


if(is_array($detail)) { 
    $data = array(); 

     $stmt = $this->mysqli->prepare("SELECT ".implode(", ",$detail)." FROM `$table` WHERE `$column` = ?"); 
     if(is_numeric($value)) { 
      $stmt->bind_param('i', $value); 
     } else { 
      $stmt->bind_param('s', $value); 
     } 
     $stmt->execute(); 
     $stmt->bind_result($detail); 
     $stmt->fetch(); 
     $data[] = $detail; 
     $stmt = null; 

    return $data; 
} 
+0

這將不起作用,因爲2個原因:1.必須從查詢中刪除'''否則查詢將不正確; 2.在'bind_result'語句中,你必須分別傳遞每列。 – Zeusarm 2014-09-21 10:41:53

相關問題