Php mysqli_query()要求参数1为mysqli,字符串以

Php mysqli_query()要求参数1为mysqli,字符串以,php,mysql,Php,Mysql,这是我的php文件 <?php /* * Following code will create a new product row * All product details are read from HTTP Post Request */ // array for JSON response $response = array(); // check for required fields if (isset($_POST['userName']) && i

这是我的php文件

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */
// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['userName']) && isset($_POST['userContact']) && isset($_POST['userAddress']) && isset($_POST['userStore']) && isset($_POST['userRequest'])) {

    $userName = $_POST['userName'];
    $userContact = $_POST['userContact'];
    $userAddress = $_POST['userAddress'];
    $userStore = $_POST['userStore'];
    $userRequest = $_POST['userRequest'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();


    // mysql inserting a new row
    $result = "INSERT INTO userrequests(userName, contactNumber, userAddress, storeList, requestBody) VALUES('$userName', '$userContact', '$userAddress', '$userStore', '$userRequest')";


    // check if row inserted or not
    if (mysqli_query($result,$db)) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
        echo $result;
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "IsitdispllayingthusOops! 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);
}
?>

程序化风格
混合mysqli\u查询(mysqli$link,string$query[,int$resultmode=mysqli\u STORE\u RESULT])

第一个参数必须是由

返回的有效/活动连接资源。正如错误所述,第一个参数应该是mysqli类,而不是查询。像这样:

if (mysqli_query($db, $result)) {
用这个

mysqli_query($db,$result) //connection first.. query at second position
而不是

mysqli_query($result,$db)
一定是这样

$result = "INSERT INTO userrequests(userName, contactNumber, userAddress, storeList, requestBody) VALUES('$userName', '$userContact', '$userAddress', '$userStore', '$userRequest')";
    // check if row inserted or not
    if (mysqli_query($db,$result)) {
    }

修改db connect类以维护连接:

class DB_CONNECT {

    protected $connection = null;    

    // constructor
    function __construct() {
        // connecting to database
        $this->connection = $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, DB_DATABASE) 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;
    }

    public function getConnection() {
        return $this->connection;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection

    }

}
然后将查询行修改为:

if (mysqli_query($db->getConnection(), $result)) {

在过程风格中,可能重复查看参数的顺序,然后查看arguments@MarkBaker我已将顺序更改为此if(mysqli_query($db,$result)),但仍然不起作用。您的db_connect建立了一个连接,但随后忽略了它,这也是一个问题。。。。您的$db不是连接本身,只是db_connect classyes的一个实例。我想这与我的db_connect.php有关。你能找出哪里出了问题吗?我想@Mark baker有答案!尝试他的答案,如果不起作用,请告诉我
$db
参数随
var_dump()返回了什么就像一个符咒。谢谢。db_config.php文件中有什么?
if (mysqli_query($db->getConnection(), $result)) {