【问题标题】:mysqli_query() expects parameter 1 to be mysqli, object given in [duplicate]mysqli_query() 期望参数 1 是 mysqli,对象在 [重复]
【发布时间】:2016-03-06 20:14:41
【问题描述】:

当我尝试使用 wampServer 在 mysql 中保存数据库时出现此错误。

mysqli_query() 期望参数 1 是 mysqli,对象在

有 3 个 PHP 文件,db_config.php 和 db_connect.php 和 create_person.php,我已经在 phpmyadmin 中创建了 PERSON 表。

db_config.php

<?php
 
/*
 * All database connection variables
 */
 
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "othmane"); // database name
define('DB_SERVER', "localhost"); // db server
?>

db_connect.php

<?php
 
/**
 * A class file to connect to database
 */
class DB_CONNECT {
 
    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }
 
    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }
 
    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';
 
        // Connecting to mysql database
        $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
 
        // Selecing database
        $db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
 
        // returing connection cursor
        return $con;
    }
 
    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysqli_close();
    }
 
}
 
?>

创建 person.php :

<?php
 
/*
 * Following code will create a new person row
 * All product details are read from HTTP Post Request
 */
 
// array for JSON response
$response = array();
 
// check for required fields
if (isset($_POST['nom']) && isset($_POST['pass_world'])) {
 
    $name = $_POST['nom'];
    $pass = $_POST['pass_world'];
 
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
 
    // connecting to db
    $db = new DB_CONNECT();
 
    // mysql inserting a new row
    $result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");
 
    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";
 
        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
 
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}
?>

【问题讨论】:

  • 只是一个小的修正。我们不检查if (isset($_POST['nom']) ... 的必填字段,因为即使它们仍然是空的,它们仍然被设置。即空也是一个值。试试($_POST['nom'] != '')类似的东西

标签: php mysqli


【解决方案1】:

你的

$db = new DB_CONNECT();

返回一个 DB_CONNECT 类的实例。

你需要什么

mysqli_query(...)

call 是您的 mysqli_connect 调用返回的对象。 尝试更改您的:

function __construct() {
    // connecting to database
    $this->connect();
}

与:

function __construct() {
    // connecting to database
    $this->conn = $this->connect();
}

然后,改变:

$result =mysqli_query($db,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");

与:

$result =mysqli_query($db->conn,"INSERT INTO person (nom,pass_world) VALUES ('$name', '$pass')");

另外,我认为您应该阅读 PHP 手册以了解您正在使用的功能: mysqli_connect mysqli_select_db

【讨论】:

  • 谢谢你将行保存到表中,但我仍然在 close() 方法中遇到错误,我尝试了这个 $this->con = $this->close();但它不起作用
  • 解决了我应该使用 mysqli_close($this->con);谢谢你是对的我应该阅读PHP手册
  • 是的!始终阅读 PHP 手册。大多数时候,你会在那里找到答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
相关资源
最近更新 更多