0

I got this error when i try to save database in mysql using wampServer.

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

there are 3 PHP files, db_config.php and db_connect.php and create_person.php and i already reated table PERSON in phpmyadmin.

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

create 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);
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Otmàane Fikri
  • 109
  • 3
  • 14
  • Just a small correction. we dont check required field by `if (isset($_POST['nom']) ...` because even when they are still empty they are still set. i.e. empty is also a value. Try if `($_POST['nom'] != '')` something like that – Omari Victor Omosa Mar 06 '16 at 20:21

1 Answers1

2

Your

$db = new DB_CONNECT();

returns an instance of DB_CONNECT class.

What you need in your

mysqli_query(...)

call is the object returned by your mysqli_connect call. Try changing your:

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

with:

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

Then, change:

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

with:

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

Also, I think you should read the PHP manual for the functions you are using: mysqli_connect mysqli_select_db

gmarintes
  • 1,288
  • 12
  • 16