-3

I have a file that contains all the information for my database. I have another file that uses this information. And I have a function that is suppose to gather information from the database but I need to use mysqli_query.

How do I do this? mysqli_query expects two things:

  1. the query
  2. the connection to the database

But my connection to the database is a separate function.

private $db;

//put your code here
// constructor
function __construct() {
    require_once 'DB_Connect.php';
    // connecting to database
    $this->db = new DB_Connect();
    $this->db->connect();
}

// destructor
function __destruct() {

}

public function isUserExisted($email) {
    $result = mysqli_query(

What goes here?

        ,"SELECT email from users WHERE email = '$email'");
    $no_of_rows = mysqli_num_rows($result);
    if ($no_of_rows > 0) {
    // user existed 
    return true;
    } else {
    // user not existed
    return false;
    }
}
Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
  • 2
    Try the documentation: http://php.net/manual/en/mysqli.query.php – Matthew Oct 02 '13 at 02:05
  • Does this answer your question? [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – Dharman Jan 04 '20 at 21:06

2 Answers2

-1
$result = mysqli_query($this->db->connect(),"SELECT email from users WHERE email = '$email'");
Ronny Morán
  • 513
  • 6
  • 9
-2

You Should initiate connection object and use it for query Your Construct Function should be like this

function __construct() {
          require_once 'DB_Connect.php';
           $db = new DB_Connect;
           $this->db_con = $db->connect();
       }

and then you can call anywhere in class like this:

$result = $this->db_con->query("select * from users Where username ='$username' and pass='$hashedpassword'");
lemon
  • 2,990
  • 8
  • 28