警告:mysqli_error()期望参数1为mysqli,null为null

时间:2016-10-02 10:25:51

标签: php mysql mysqli

警告:mysqli_error()期望参数1为mysqli,在尝试将csv文件上传到数据库时,错误指示为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);
}

}

&GT;

1 个答案:

答案 0 :(得分:2)

看看这一行:

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

您正在使用$db_mysqli_connection进行连接,但$connection用于检查错误。您必须将$connection替换为$db_mysqli_connection