简体   繁体   English

Mysqli查询不起作用但没有错误

[英]Mysqli query doesn't work but no errors

I'm trying to insert data into db from php 我正在尝试将数据从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;
}

I wrote this function but the code inside the if statement is never executed, I get only the echo without error because $this->conn->error return null. 我写了这个函数,但是if语句中的代码从未执行,我只得到没有错误的回声,因为$this->conn->error返回null。 The other function called is composeInsertQuery(...): 另一个调用的函数是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;
}

It compose the query in a modular way because I need to have different number of fields every time. 它以模块化的方式构成查询,因为我每次都需要具有不同数量的字段。 However this is not the problem because I tried to print the composed query and assign it directly to a variable, like this: 但这不是问题,因为我尝试打印组合查询并将其直接分配给变量,如下所示:

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;
}

But even in this case I got the echo with $this->conn->error always empty string. 但是即使在这种情况下,我也得到了$this->conn->error总是空字符串的回显。 Of course the same query works correctly from terminal. 当然,同一查询可以从终端正常工作。

$this->conn->query return NULL and this is weird according to PHP documentation: $this->conn->query返回NULL,根据PHP文档,这很奇怪:

Returns FALSE on failure. 失败时返回FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN > queries mysqli_query() will return a mysqli_result object. 为了成功执行SELECT,SHOW,DESCRIBE或EXPLAIN>查询,mysqli_query()将返回mysqli_result对象。 For other > successful queries mysqli_query() will return TRUE. 对于其他>成功的查询,mysqli_query()将返回TRUE。

删除composeInsertQuery()中的id字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM