2009-09-02 137 views
2

我沒有看到的錯誤,並希望有人能搞清楚:準備好的聲明中通過引用傳遞錯誤

public static function createMessage($title, $message, $startDate, $endDate, $author, $status){ 
     //$dbConn is now a mysqli instance with a connection to the database foobar 
     $dbConn = Database::getConnection("foobar"); 

     $stmt = $dbConn->prepare("INSERT into messages(title, msg, date_start, date_end, app_usersID_FK, date_created, activeflag, msg_status) VALUES (?,?,?,?,?,?,?,?)"); 

     if(!$stmt){ 
      throw new Exception("Unable to prepare the statement"); 
     } 

     $dt = date("Y-m-d"); 
     $stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, 1, $status); 
     $stmt->execute(); 

     return true; 
} 

函數調用

MessageCenter ::的CreateMessage(「你好」,「只要打電話來打個招呼「,」2009-09-12「,」2009-09-12「,」1「,」1「);

錯誤消息:

致命錯誤:無法通過引用

回答

6

我猜你的bind_param方法實際上是mysqli_stmt::bind_param傳遞參數8。如果是:每個參數(除了第一個參數)都必須是通過引用傳遞的變量,因此它們可以被「綁定」。

手動像說(重點煤礦)

mysqli_stmt::bind_param -- mysqli_stmt_bind_param — Binds variables to a prepared statement as parameters


這意味着你不能傳遞一個值:你必須使用變量。

硅,在你的情況下,這樣的事情應該做的:

$my_var = 1; 
$stmt->bind_param("ssssisii", $title, $message, $startDate, 
    $endDate, $author, $dt, $my_var, $status); 
+0

尼斯徹底響應 – 2009-09-02 19:32:27

+0

@Peter:感謝:-) – 2009-09-02 19:34:07

0

找到了!它希望activeFlag是一個變量。以下作品:

$dt = date("Y-m-d"); 
$flag = 1; 
$stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, $flag, $status); 
$stmt->execute();