警告:mysqli_query():无法在我的代码中获取mysqli

时间:2015-11-03 06:38:03

标签: php mysqli

我知道这个问题已被多次询问过,我尝试了很多其他答案,但它并没有为我工作

这是我的班级

<?php 

class saveexceltodb
{

    var $inputFileName;
    var $tableName;
    var $conn;
    var $allDataInSheet;
    var $arrayCount;

    /**
     * Create a new PHPExcel with one Worksheet
     */
    public function __construct($table=0)
    {
          $this->initiatedb();
    }


    private function initiatedb(){

        //var_dump($allDataInSheet);
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "xyx";

            // Create connection
            $this->conn = mysqli_connect($servername, $username, $password, $dbname);

            // Check connection
            if (!$this->conn) {
                die("Connection failed: " . mysqli_connect_error());
            }

    }


    public function updateIntermidiate(){

        $allocationeventwise=array();

        mysqli_query($this->conn,"DELETE FROM `allocationeventwise` WHERE 1");

         $sql = "INSERT INTO `allocationeventwise` (`empid`, `event1`, `event2`, `event3`, `event4`, `event5` `event6`, `event7`) VALUES ";
        $result = mysqli_query($this->conn, "SELECT usdeal.empid , usdeal.event1 , usdeal.event2, usdeal.event3, usdeal.event4, usdeal.event5, usdeal.event6, usdeal.event7 , salary.salary from usdeal INNER JOIN salary ON usdeal.empid = salary.empid where usdeal.allocated=0");

        $i=0;
        if (mysqli_num_rows($result) > 0) {

            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                 $i++;
                 $totaleventDays = $row["event1"]+$row["event2"]+$row["event3"]+$row["event4"]+$row["event5"]+$row["event6"]+$row["event7"];

                 $allocationeventwise[$row["empid"]]['event1']=($row['salary']/$totaleventDays)*$row["event1"];
                 $allocationeventwise[$row["empid"]]['event2']=($row['salary']/$totaleventDays)*$row["event2"];
                 $allocationeventwise[$row["empid"]]['event3']=($row['salary']/$totaleventDays)*$row["event3"];
                 $allocationeventwise[$row["empid"]]['event4']=($row['salary']/$totaleventDays)*$row["event4"];
                 $allocationeventwise[$row["empid"]]['event5']=($row['salary']/$totaleventDays)*$row["event5"];
                 $allocationeventwise[$row["empid"]]['event6']=($row['salary']/$totaleventDays)*$row["event6"];
                 $allocationeventwise[$row["empid"]]['event7']=($row['salary']/$totaleventDays)*$row["event7"];
                 $sql .='("'.$row["empid"].'",
                        '.$allocationeventwise[$row["empid"]]["event1"].',
                        '.$allocationeventwise[$row["empid"]]["event2"].',
                        '.$allocationeventwise[$row["empid"]]["event3"].',
                        '.$allocationeventwise[$row["empid"]]["event4"].',
                        '.$allocationeventwise[$row["empid"]]["event5"].',
                        '.$allocationeventwise[$row["empid"]]["event6"].',
                        '.$allocationeventwise[$row["empid"]]["event7"].',)';

                 if($i<mysqli_num_rows($result))
                    $sql .=",";
                 else
                    $sql .=";";
            }

            echo $sql;
        if (mysqli_query($this->conn, $sql)) {
                    echo "New record created successfully<br/>";
                } else {
                    echo "Error: " . $sql . "<br>" . mysqli_error($this->conn);
                }

        } else {
                    echo "0 results";
        }




    }

}

当我像这样使用它时

include('saveexceltodb.php');
$obj = new saveexceltodb(0);

$obj->updateIntermidiate();

我收到以下错误。

Warning: mysqli_query(): Couldn't fetch mysqli in C:\xampp\htdocs\import-excel\saveexceltodb.php

1 个答案:

答案 0 :(得分:-1)

您应该按照以下步骤

  1. 检查数据库连接
  2. 检查查询语法
  3. 你需要在mysqli_query函数中传递连接对象,例如它是&#39; $ this-&gt; conn&#39;
  4. 希望,它可能对你有所帮助。