警告:mysqli_query()期望参数1为mysqli,给定对象

时间:2015-11-05 09:54:38

标签: php mysql mysqli

我遇到此错误的问题,我使用的代码如下:

<?php
$cus_name = isset($_POST['client_name'])?$_POST['client_name']:'';
$description = isset($_POST['desc'])?$_POST['desc']:'';
$amount = isset($_POST['amnt'])?$_POST['amnt']:'';

$query = "INSERT INTO ".$RECORD_TABLE."(cus_name,description,amount) VALUES ('$cus_name','$description','$amount') " ;
$result = mysqli_query($dbObj,$query);

if(!$result)
{
  echo "error while inserting";
}


?>

这是我用于将日期插入数据库的代码。我在下面给出的连接代码:

$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PWD = '';
$DB_NAME = 'hotel_booking';

$BOOKING_TABLE = 'hotel_booking';
$PRICING_TABLE = 'hotel_pricing';
$SETTING_TABLE = 'hotel_setting';
$RECORD_TABLE = 'hotel_record';
require_once('Database.class.php');

global $dbObj;
$dbObj = new Database($DB_HOST,$DB_USER,$DB_PWD,$DB_NAME,1,0);

3 个答案:

答案 0 :(得分:1)

我认为你的问题是连接 试试这段代码

$conn = mysqli_connect($DB_HOST,$DB_USER,$DB_PWD,$DB_NAME);

并且还要改变

$query = "INSERT INTO ".$RECORD_TABLE."(cus_name,description,amount) VALUES ('".$cus_name."','".$description."','".$amount."') " ;
$result = $conn->query($query);

答案 1 :(得分:0)

现在您的$dbObjDatabase类的对象。如果您要使用mysqli_*,则$dbObj应该是这样的。

$dbObj = mysqli_connect("host", "user", "password","database name");

有关详细信息,请参阅here

答案 2 :(得分:-1)

检查班级是否返回新的mysqli

 class Database() {
    function __constructor(){
        $obj=mysqli_connect($DB_HOST,$DB_USER,$DB_PWD,$DB_NAME);
        return $obj;
    }
 }