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.