問題描述
當我嘗試從文本文件導入數據時,我在以下函數中收到無法獲取 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 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!