简体   繁体   中英

mysqli_query() expects parameter 1 to be mysqli, string given in

this is my php file

<?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);
}
?>

And this is my 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, 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;
    }

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

    }

}



?>

It seems that there's something wrong with my code and this is the error I got mysqli_query() expects parameter 1 to be mysqli, string given in. Anyone knows how to solve this ? I have searched through StackOverFlow regarding this and tried all the solutions but still cant be solved.

http://docs.php.net/mysqli_query :

Procedural style
mixed mysqli_query ( , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

The first parameter must be a valid/active connection resource that has been returned by mysqli_connect()

As the error said, your first parameter should be the mysqli class, not the query. Like this:

if (mysqli_query($db, $result)) {

use this

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

instead of

mysqli_query($result,$db)

It has to be like this

$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)) {
    }

Modify your db connect class to maintain the connection:

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

    }

}

Then modify your query line to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM