简体   繁体   English

mysqli_query 返回 false,错误代码为 0,但查询成功

[英]mysqli_query returns false with error code 0, but query succeeds

I have a huge amount of data that is generated from a PHP script and needs to be inserted into a database.我有大量数据是从 PHP 脚本生成的,需要插入到数据库中。 I've tried various solutions with different results, but the current solution (and the one I think should be the best) is that i generate the data into a CSV file and then inserts it into the database with the following query:我尝试了各种不同结果的解决方案,但当前的解决方案(我认为应该是最好的解决方案)是我将数据生成到一个 CSV 文件中,然后使用以下查询将其插入到数据库中:

LOAD DATA LOCAL INFILE 'myfile.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'"

I'm using CodeIgniter as the PHP framework, and after the query has executed im redirected to an error page which only says我使用 CodeIgniter 作为 PHP 框架,在执行查询后,我重定向到一个错误页面,该页面只显示

A Database Error Occurred发生数据库错误

Error Number: 0错误编号:0

There is no error message or anything.没有错误消息或任何东西。

I've stepped through the code but all I can find is that mysqli_query() returns false and later on mysqli_errno() returns 0 and mysqli_error() returns an empty string.我已经遍历了代码,但我能找到的只是mysqli_query()返回 false,后来mysqli_errno()返回 0,而mysqli_error()返回一个空字符串。

However , the query has actually succeeded and when I look in the database i can see that all the data from the CSV file have successfully been inserted.然而,查询实际上已经成功,当我查看数据库时,我可以看到 CSV 文件中的所有数据都已成功插入。 Is this behaviour to be expected?这种行为是可以预期的吗? If so, I guess I have to hack the CodeIgniter code a little, or call mysqli_query() directly instead of going through the framework.如果是这样,我想我必须稍微破解 CodeIgniter 代码,或者直接调用mysqli_query()而不是通过框架。

I've also run the exact same query in MySQL Workbench, and I do not get an error message there.我还在 MySQL Workbench 中运行了完全相同的查询,但没有收到错误消息。

I had the exact same issue, when I realized I was not connected to the database. 当我意识到我没有连接到数据库时,我遇到了完全相同的问题。

I was calling require_once('connect.php') , but this was the second time in the code that I used the require_once , so PHP did not pull in the connection. 我在调用require_once('connect.php') ,但这是我在代码中第二次使用require_once ,所以PHP没有require_once连接。

Changing the require_once to require fixed this issue for me. 更改require_once以要求我解决此问题。

I have never tried a LOAD INFILE from php before, but a quick google came up with the function mysqli_set_local_infile_handler() . 我之前从未尝试过来自php的LOAD INFILE ,但是快速谷歌提出了函数mysqli_set_local_infile_handler() Perhaps the examples/comments might help? 也许这些例子/评论可能有所帮助?

Edit: My theory is the query expects a number of rows return value, and the connection isn't returning any numbers. 编辑:我的理论是查询期望多行返回值,并且连接不返回任何数字。 So while the query is successful on MySQL's end, PHP expects a number > 0 but doesn't get it. 因此,虽然在MySQL结束时查询成功,但PHP期望数字> 0但不能得到它。 So it reports a failure, with no error message. 因此它报告失败,没有错误消息。

The infile_handler would return the number of rows inserted. infile_handler将返回插入的行数。

Error (0) at some instances may just be misleading.错误 (0) 在某些情况下可能只是误导。 For example, when returned during a mysqli update, it might simply mean the changes were done but compared to the last records, nothing actually changed .例如,当在 mysqli 更新期间返回时,它可能只是意味着更改已完成,但与最后的记录相比,实际上没有任何更改 This could be the issue you are facing.这可能是您面临的问题。 An example with mysqli update is shown below: mysqli 更新的示例如下所示:

$xCount = 0;        //will monitor if changes were done
$updateFlag = false;
$xMsg = '';
$dbx = $this->dbx;
$dbx->select_db("dbname");
$query = "UPDATE xxx_table SET NAME = ?, GENDER = ? WHERE ORG_ID = ? AND EMPID = ?";
$statement = $dbx->prepare($query); 
$statement->bind_param('ssss', $flname, $gender, $orgId, $empId);
if($statement->execute()){
    $xCount = $statement -> affected_rows;  //real-check if changes occured
    if($xCount > 0){
        $updateFlag = true; 
        $xMsg = 'Employee Data Successfully Updated';
    }else {
        //if no changes occur, like same values, this will run
        //in this particular case, your may treat as fine 
        $updateFlag = true; 
        $xMsg = "Successful With No Major Changes";
    }       
} else {
    //this is simply your coding error;
    //die('Error : ('. $dbx->errno .') '. $dbx->error); 
    $updateFlag = false;
    $xMsg = 'Internal Error On Employee Logs';
}
$statement->close();

I hope this helps on a related problem, may be not this particular one.我希望这有助于解决相关问题,可能不是这个问题。

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

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