[英]Mysqli query doesn't work but no errors
我正在嘗試將數據從php插入db
public function writeIntoDB($fields, $values, $table){
$query = $this->composeInsertQuery($fields, $values, $table);
if (($this->conn->query($query)) === TRUE){
...
}
echo "Error: " . query . "<br>" . $this->conn->error;
}
我寫了這個函數,但是if語句中的代碼從未執行,我只得到沒有錯誤的回聲,因為$this->conn->error
返回null。 另一個調用的函數是composeInsertQuery(...):
private function composeInsertQuery($fields, $values, $table){
$query = "INSERT INTO " . $table . " (id,";
foreach ($fields as $value) {
$query .= $value . ",";
}
$query = substr($query, 0, -1) . ") VALUES ('$this->key',";
foreach ($values as $value) {
$query .= "'$value',";
}
$query = substr($query, 0, -1) . ")";
return $query;
}
它以模塊化的方式構成查詢,因為我每次都需要具有不同數量的字段。 但這不是問題,因為我嘗試打印組合查詢並將其直接分配給變量,如下所示:
public function writeIntoDB($fields, $values, $table){
$query = "INSERT INTO ... all the query ....";
if (($this->conn->query($query)) === TRUE){
...
}
echo "Error: " . query . "<br>" . $this->conn->error;
}
但是即使在這種情況下,我也得到了$this->conn->error
總是空字符串的回顯。 當然,同一查詢可以從終端正常工作。
$this->conn->query
返回NULL,根據PHP文檔,這很奇怪:
失敗時返回FALSE。 為了成功執行SELECT,SHOW,DESCRIBE或EXPLAIN>查詢,mysqli_query()將返回mysqli_result對象。 對於其他>成功的查詢,mysqli_query()將返回TRUE。
刪除composeInsertQuery()中的id字段
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.