mysqli_query 返回 false,错误代码为 0,但查询成功
我有大量由 PHP 脚本生成的数据,需要插入到数据库中。我尝试了各种不同结果的解决方案,但当前的解决方案(我认为应该是最好的解决方案)是我将数据生成到 CSV 文件中,然后使用以下查询将其插入数据库
LOAD DATA LOCAL INFILE 'myfile.csv' INTO TABLE t1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'"
:使用 CodeIgniter 作为 PHP 框架,在执行查询后,我重定向到一个错误页面,该页面仅显示
发生数据库错误
错误号:0
没有错误消息或任何内容。
我已经单步执行了代码,但我能发现的只是 mysqli_query()
返回 false,后来 mysqli_errno()
返回 0 和 mysqli_error() 返回一个空字符串。
但是,查询实际上已经成功,当我查看数据库时,我可以看到 CSV 文件中的所有数据都已成功插入。这种行为是预期的吗?如果是这样,我想我必须稍微修改一下 CodeIgniter 代码,或者直接调用 mysqli_query() ,而不是通过框架。
我还在 MySQL Workbench 中运行了完全相同的查询,并且没有收到错误消息。
I have a huge amount of data that is generated from a PHP script and needs to be inserted into a database. 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:
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
A Database Error Occurred
Error Number: 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.
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. 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.
I've also run the exact same query in MySQL Workbench, and I do not get an error message there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我意识到我没有连接到数据库时,我遇到了完全相同的问题。
我正在调用
require_once('connect.php')
,但这是我在代码中第二次使用require_once
,因此 PHP 没有拉入连接。将
require_once
更改为 require 为我解决了这个问题。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 therequire_once
, so PHP did not pull in the connection.Changing the
require_once
to require fixed this issue for me.我以前从未尝试过从 php 加载 INFILE,但是快速谷歌就想出了这个函数 mysqli_set_local_infile_handler() 。也许例子/评论可能有帮助?
编辑:我的理论是查询期望许多行返回值,并且连接不返回任何数字。因此,虽然 MySQL 端查询成功,但 PHP 期望数字 > 0 但没明白。因此它报告失败,但没有错误消息。
infile_handler 将返回插入的行数。
I have never tried a
LOAD INFILE
from php before, but a quick google came up with the function 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. So it reports a failure, with no error message.
The infile_handler would return the number of rows inserted.
在某些情况下,错误 (0) 可能会产生误导。例如,当在 mysqli 更新期间返回时,它可能只是意味着更改已完成,但与最后的记录相比,实际上没有任何更改。这可能是您面临的问题。下面显示了 mysqli 更新的一个示例:
我希望这有助于解决相关问题,可能不是这个特定问题。
Error (0) at some instances may just be misleading. 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. This could be the issue you are facing. An example with mysqli update is shown below:
I hope this helps on a related problem, may be not this particular one.