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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)