2015-11-03 71 views
0

我知道這個問題已經被問了很多次,我試過很多其他的答案,但它並沒有爲我警告:mysqli_query():在我的代碼無法獲取的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

您應該按照以下步驟

  1. 檢查數據庫連接
  2. 檢查查詢語法
  3. 你要通過連接對象mysqli_query功能在你情況下,「$這個 - >康恩「

希望,它可以幫助你。

相關問題