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

查看:193
本文介绍了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>\n<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);

第7行出现无法获取mysqli"错误.第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天全站免登陆