无法识别fetch_assoc方法

时间:2015-09-01 06:58:59

标签: php mysqli

我正在尝试将excel文件上传到我的网站并将数据保存在我的数据库中,但是我没有这样做并且得到:致命错误:调用未定义的方法mysqli :: fetch_assoc()...但是我不知道如何处理它,我还没有找到与SO有关的问题,有什么帮助吗?

function getSchedule($filepath,$con,$filename){

    require_once 'excel/PHPExcel/IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load($filepath); 
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        $worksheetTitle     = $worksheet->getTitle();
        $highestRow         = $worksheet->getHighestRow(); // e.g. 10
        $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

        list($location, $date) = explode('-', $filename, 2);
        $LastChange = date('d/m/Y h:i:s');
        $Status='Open';

$servername = "localhost";
$username = "root";
$password = "Js";
$dbname = "jr";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM Schedule";
$conn->query($sql);


    // output data of each row
    while($row = $conn->fetch_assoc()) {


         $sql1="DELETE FROM `schedule` WHERE " . $row["Date"]. "='$date'";   
       $result = mysqli_query($conn,$sql1);

    }

$conn->close();


        for ($row = 3; $row <= $highestRow; ++ $row) {
            $sql="INSERT INTO `schedule` (`Status`,`LastChange`, `Location`,`Date`,`AFNumber`,`Name`,`01-IN`, `01-OUT`, `02-IN`, `02-OUT`, `03-IN`, `03-OUT`, `04-IN`, `04-OUT`, `05-IN`, `05-OUT`, `06-IN`, `06-OUT`, `07-IN`, `07-OUT`, `08-IN`, `08-OUT`, `09-IN`, `09-OUT`, `10-IN`, `10-OUT`, `11-IN`, `11-OUT`, `12-IN`, `12-OUT`, `13-IN`, `13-OUT`, `14-IN`, `14-OUT`, `15-IN`, `15-OUT`, `16-IN`, `16-OUT`, `17-IN`, `17-OUT`, `18-IN`, `18-OUT`, `19-IN`, `19-OUT`, `20-IN`, `20-OUT`, `21-IN`, `21-OUT`, `22-IN`, `22-OUT`, `23-IN`, `23-OUT`, `24-IN`, `24-OUT`, `25-IN`, `25-OUT`, `26-IN`, `26-OUT`, `27-IN`, `27-OUT`, `28-IN`, `28-OUT`, `29-IN`, `29-OUT`, `30-IN`, `30-OUT`, `31-IN`, `31-OUT`)  VALUES ('".$Status."', '".$LastChange."','".$location."','".$date."',";
            for ($col = 0; $col < ($highestColumnIndex -1); ++ $col) {
                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                $val = $cell->getValue();
                if($col==($highestColumnIndex -2)){
                     $sql.="'$val'";
                }else{
                    $sql.="'$val', ";}
            }
            echo "Index:".$highestColumnIndex."<br>";
            if($highestColumnIndex < 63){
                $temp = 63 - $highestColumnIndex;
                for($i = 1;$i <= $temp; $i++){
                    if($i == $temp){
                        $sql.=",''";
                    } else{
                    $sql.=", '',";
                    }
                }
            }
            $sql .=")";
            if ($con->query($sql) === TRUE) {
            } else {
                echo "<br><br>Error: " . $sql . "<br>" . $con->error;
            }
        }//End For Each Row
    }//End For Each Worksheet
}//End getHours Function

3 个答案:

答案 0 :(得分:0)

来自php.net

  

数组mysqli_result :: fetch_assoc(void)

这意味着您应该为其提供查询结果,而不是连接。将您的代码更改为此代码,它应该可以正常工作

$results = $conn->query($sql); //assign query to a variable and get mysqli_result in return

    while($row = $results->fetch_assoc()) { //use that in the while loop

答案 1 :(得分:0)

您正在覆盖连接对象$conn。使用

$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {

答案 2 :(得分:0)

试试这个:

   $sql = "SELECT * FROM Schedule";
    $result = $conn->query($sql);


// output data of each row
while($row = $result->fetch_assoc()) {