2016-10-02 72 views
-2

中給出警告:mysqli_error()期望參數1爲mysqli,如果在嘗試將csv文件上載到數據庫時發生錯誤,則返回null。警告:mysqli_error()期望參數1爲mysqli,null在

下面是我的代碼片段。我不在哪裏,我可能會失火。

<?php 

class UploadFIle 
{ 
    function __construct() 
{ 

} 
public function insertFile($filename,$tableName){ 
    global $db_mysqli_connection; 
    $queryTruncate="TRUNCATE table ".$tableName; 
    mysqli_query($db_mysqli_connection,$queryTruncate); 
    $handle = fopen($filename, "r"); 
    $query='INSERT INTO '.$tableName.' VALUES '; 

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $limit=sizeof($data); 
     $query.='('; 
     $counter=0; 
     while($limit>0){ 
      $query.="\"".$data[$counter]."\"".','; 
      ++$counter; 
      --$limit; 
     } 

     $query=rtrim($query,','); 

     $query.='),'; 

    } 
    $query=rtrim($query,','); 
    mysqli_query($db_mysqli_connection,$query) or die(mysqli_error($connection).$query); 

     if (mysqli_affected_rows($db_mysqli_connection)<=0) { 
      // echo "\nQuery execute failed: ERRNO: (" . $mysqli->errno . ") " . $mysqli->error; 
      $csvMessage = "Upload Failed <br/>" . $db_mysqli_connection->error ; 
     }else{ 
      $csvMessage = "Upload Successful"; 
     } 
     return $csvMessage; 
     //var_dump($mysqli); 
} 

}

>

+0

'var_dump($ db_mysqli_connection)' –

+0

它表示您的$ db_mysqli_connection爲空。它在哪裏定義? – Turtle

+0

@turtle定義爲插入文件函數定義後的全局db_mysqli_connection –

回答

1

看這句話?

mysqli_query($db_mysqli_connection,$query) or die(mysqli_error($connection).$query); 

您正在使用$db_mysqli_connection連接,但$connection檢查錯誤。您必須將$connection替換爲$db_mysqli_connection

+0

漂亮的抓人! – Turtle

相關問題