mysqli_query(): 无法获取 mysqli 错误 [英] mysqli_query(): Couldn't fetch mysqli error

查看:71
本文介绍了mysqli_query(): 无法获取 mysqli 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从文本文件导入数据时,我在以下函数中收到无法获取 mysqli"错误.然而,插入功能在其他地方工作得很好.文件中的数据被正确解析,当我将其复制并粘贴到 PHPMyAdmin 时,查询工作正常.

I am receiving a "Couldn't fetch mysqli" error on the following function when trying to import data from a text file. However the insert function is working just fine in other places. The data from the file is being parsed correctly, and the query works fine when I copy and past it into PHPMyAdmin.

功能

1. $conn = mysqli_connect("localhost","xxxxx","xxxxx","xxxx") OR die("Error!!");
2. function insert ($query){
3.  global $conn;
4.  if (!$conn) {
5.          die("Connection failed: " . mysqli_connect_error());
6.  }
7.  if (mysqli_query($conn, $query)){
8.      $result = "<b>Inserted $query</b>
<br />";
9.      return $result;
10. } else {
11.     echo "Error: ". $query . "<br />" . mysqli_error($conn);
12. }
13. 
14. mysqli_close($conn);
15. }

我要插入的查询是:

INSERT INTO movies VALUES( '2603', 'Miracle Season, The', 'The Miracle Season', '2019-04-01',
'2D', '9', '101', '10', 'PG', 'for some thematic elements.', '', 'Scope', '01:30:55' )

我通过 $result = insert($query); 调用函数;

I am calling the function by $result = insert($query);

无法获取 mysqli"错误在第 7 行.我也在第 11 行和第 14 行收到错误.

The "couldn't fetch mysqli" error is on line 7. I'm also getting the error on line 11 and line 14.

正如我上面所说,该函数与其他脚本一起运行良好,当我尝试手动运行时,查询运行良好.

As I said above, the function is working fine with other scripts and the query works fine when I try to run it manually.

另外一个注意事项是插入的项目不包含自动递增 id,因为查询的第一个值已经是来自另一个数据库的唯一 id.我的导入函数检查 id 以查看它是否已经存在,并根据行是否存在运行更新或插入查询.

One additional note is the inserted items do not contain an auto increment id because the first value of the query is already a unique id from another database. My import function checks the id to see if it already exists and runs an update or insert query depending on whether the row exists.

推荐答案

我怀疑您多次调用该函数.但是它在最后调用了mysqli_close($conn),所以当你下次尝试使用它时出现错误,因为$conn不能使用任何更多.

I suspect you're calling the function multiple times. But it calls mysqli_close($conn) at the end, so when you try to use it the next time you get an error, because $conn can't be used any more.

去掉那一行,用 MySQL 脚本全部完成后关闭连接(或者不用管,脚本结束时它会自动关闭).

Get rid of that line, and close the connection when the script is all done using MySQL (or don't bother, it will be closed automatically when the script ends).

这篇关于mysqli_query(): 无法获取 mysqli 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆