i am developing a login system in php with oops. I am not able to pass the connection to the query so the query is not processing.
below is the files : config.php
<?php
define("DB_HOST",'localhost');
define("DB_USER",'root');
define("DB_PASSWORD",'');
define("DB_DATABASE",'admin_safeplace');
?>
dbConnect.php--->for database connection
<?php
class dbConnect
{
public $conn;
function __construct()
{
//Initialize connection to database
require_once('config.php');
$this->conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
if(!$this->conn)
{
die('Cannot connect to the database');
}
return $this->conn;
}
}
?>
dbFunction.php--->For various function
<?php
require_once('dbConnect.php');
session_start();
class dbFunction
{
function __construct()
{
//Connecting to the database
$db=new dbConnect();
}
function login($email,$password) //Login Function
{
$pass=md5($password);
$sql="Select * from `super_user` where `email`='$email' AND `password`='$pass'";
$res=mysqli_query($db->conn,$sql);
$user_data = mysqli_fetch_array($res);
$rowCount=mysqli_num_rows($res);
if($rowCount==1)
{
$_SESSION['login']=true;
$_SESSION['email']=$user_data['email'];
return TRUE;
}
else
{
return FALSE;
}
}
}
?>
login page code:
<!--Login Code-->
<?php
$funObj = new dbFunction();
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$user = $funObj->login($email, $password);
if ($user)
{
// Login Success
header("location:list_users.php");
}
else
{
// Login Failed
echo "<script>alert('Emailid / Password Not Match')</script>";
}
}
?>
How can i pass the $conn i.e connection to the query in login() function.can anyone help ?