2017-08-10 135 views
-3

我正在使用以下代碼。但是我收到了這個警告信息。我不知道爲什麼會發生這種情況。警告mysqli_query()至少需要2個參數

mysqli_query() 期望至少2個參數....

配置

class org_type 
{ 
    private $conn = ''; 
    function __construct() 
    { 
     global $con; 
     $this->conn = $con; 
    } 

    public function create(){ 
     $this->create_org_type(); 
    } 

    $sql = "CREATE TABLE IF NOT EXISTS `org_type` 
     (
     `orgTypeId` int NOT NULL AUTO_INCREMENT, 
     PRIMARY KEY(orgTypeId), 
     `orgTypeName` text, 
     `status` varchar(150), 
     `create_date` INT(11), 
     `desp` TEXT DEFAULT '' 
     )"; 
    mysqli_query($this->conn,$sql); 

}*/ 

public function insert_orgType($orgName,$sts,$editVal){ 
    $now = time(); 
    mysqli_query($this->conn, "INSERT INTO `org_type`(orgTypeName,status,create_date,desp) VALUES ('".$orgName."','".$sts."',".$now.",'".$editVal."')") or die (mysqli_error($this->conn)); 
    $insId = mysqli_insert_id($this->conn); 
    return $insId; 

} 

    public function select_orgType($id=0){ 
     $str = ''; 
     $arrResult = array(); 


     if($id != 0){ 
      $str .= " AND orgTypeId = $id ";   
     }    
     $sql = "SELECT * FROM org_type WHERE 1 $str order by orgTypeId ASC"; 
     $result = $this->conn->query($sql); 
if ($result->num_rows > 0) { 
      while($row = $result->fetch_assoc()) { 
      $arrResult[] = $row; 
     } 
     return $arrResult; 
     } 
    }  
} 
+0

你還沒有使用的連接'$這個 - > conn'在'insert_orgType' –

+0

使用。如果我使用$ insId查詢= mysqli_insert_id($ this-> conn),得到相同的錯誤 – Ived

+0

'mysqli_query($ this-> conn,QUERY)' –

回答

0

mysqli_query()需要連接變量傳遞。 更新查詢如下:

mysqli_query($this->conn,"INSERT INTO `org_type`(orgTypeName,status,create_date,desp) VALUES ('".$orgName."','".$sts."',".$now.",'".$editVal."')") or die (mysqli_error()); 
1

你需要傳遞的連接對象作爲參數用於以下功能

1:mysqli_query()需要兩個參數

第一個是connection object

第二個是query

mysqli_query($this->conn,query); 

第二:mysqli_error()

需要pass connection objectmysqli_error($this->conn)功能

3:mysqli_insert_id()

需要pass connection objectmysqli_insert_id($this->conn)功能

更新1:需要includefile

function __construct() 
{ 
    global $con; 
    include('connection.php'); 
    $this->conn = $con; 
} 
+0

我更新了上面的代碼,仍然得到同樣的錯誤,mysqli_query()期望參數1是mysqli,null給出了..... – Ived

+1

我在'你的代碼'中沒有看到'mysql connection' creation'code' – JYoThI

相關問題