mysqli_query(): 无法获取 mysqli 错误

mysqli_query(): Couldn#39;t fetch mysqli error(mysqli_query(): 无法获取 mysqli 错误)
本文介绍了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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Warning: mysqli_query() expects at least 2 parameters, 1 given. What?(警告:mysqli_query() 需要至少 2 个参数,1 个给定.什么?)
INSERT query produces quot;Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean givenquot;(INSERT 查询产生“警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,给出布尔值;)
prepared statements - are they necessary(准备好的陈述 - 它们是否必要)
Do I need to escape my variables if I use MySQLi prepared statements?(如果我使用 MySQLi 准备好的语句,是否需要转义我的变量?)
Properly Escaping with MySQLI | query over prepared statements(使用 MySQLI 正确转义 |查询准备好的语句)
Is it possible to use mysqli_fetch_object with a prepared statement(是否可以将 mysqli_fetch_object 与准备好的语句一起使用)