Reputation: 337
I'm trying to make my login homepage work. At the first I built my query with mysql but I always got the error "query is empty" so I decided to change over to mysqli. I'm now getting the error "Warning: mysqli_query() expects at least 2 parameters". how can I call the first parameter "$db" in my "mysqli_query($db, $string)" from the method "connect()" in the class "database2" to make it work? I tried with " $result = $this->cxn->query($query)" but then i got the error "Fatal error: Call to undefined method Database2::query()
<?php
class Login
{
private $username;
private $password;
private $cxn; // Database object.
function __construct($username, $password)
{
// Set data
$this->setData($username, $password);
// connect to db
$this->connectToDb();
//get Data
$this->getData();
}
private function setData($username, $password){
$this->username = $username;
$this->password = $password;
}
private function connectToDb(){
include 'models/database2.php';
$vars = "include/vars.php";
$this->cxn = new Database2($vars);
}
private function getData(){
$query ="SELECT 'username', 'password' FROM 'users' WHERE 'username' = '$this->username'
AND 'password' = '$this->password'";
$result = mysqli_query($query) or die(mysql_error());
$num_row = mysqli_num_rows($result);
if ($num_row>1) {
return TRUE;
}else{
throw new Exception("The query was not successful!");
}
}
function close(){
$this->cxn->close_connect();
}
}
?>
Database2 class:
<?php
class Database2{
private $host;
private $user;
private $password;
private $database;
function __construct($filename){
if(is_file($filename)){
include $filename;
}else{
throw new Exception("Error Processing Request");
}
$this->host = $host;
$this->user = $user;
$this->password =$password;
$this->database =$database;
$this->connect();
}
public function connect(){
// connect to the server.
$db = new mysqli($this->host, $this->user, $this->password);
if ($db->connect_errno) {
die("We are sorry, you could not be connected to the server,
plaese check your connection setting!");
}else{
echo "You are connected to the database";
}
}
public function close_connect(){
mysql_close();
}
}
?>
i appreciate any help
Upvotes: 0
Views: 910
Reputation: 72
If you use procedural style, you have to set and use a "$db" variable in the database2 class. Set the variable in the connect function:
$this->db = new mysqli($this->host, $this->user, $this->password);
And then, you can use the $db in the database2 object
$result = mysqli_query($this->cxn->db, $query);
Upvotes: -2