1

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.

Function

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. }

The query I am trying to insert is:

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' )

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

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.

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Justin H
  • 13
  • 4

1 Answers1

3

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.

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).

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • The script I'm working with parses a long list of movies from a text file. It uses a for loop to look at each line in the file, parses up the pieces into the different column values, then either updates or inserts the row after determining it does not exist. The error is happening near the bottom of the file after it has already updates several dozen titles. My update function looks just like the insert function above, except the output is for an update result. – Justin H Apr 05 '18 at 22:01
  • Not sure why it happens after several dozen updates. It should happen on the second update, since you close the connection after the first update. – Barmar Apr 05 '18 at 22:03
  • I finally figured it out. I was closing $conn in my insert, select, and update functions. Once I removed this everything worked fine. It still doesn't answer why I was having the problem with the insert function and not the update function, but I'm not going to question it. – Justin H Apr 21 '18 at 04:25