I have three files first if config class, second is user class and third is index file.
config.php
class connection{
public function dbConnect()
{
new PDO("mysql:host=localhost:3306;dbname=education","root","");
}
}
user.php
<?php
session_start();
require_once '../cms/config.php';
class user{
private $db;
public function __construct()
{
$this->db= new connection();
$this->db= $this->db->dbConnect();
return $this->db;
}
public function login($user,$pass)
{
//creating a array for future usage
if(!empty($user) && !empty($pass))
{
$st=$this->db->prepare("select * from admin where admin_user=? AND admin_pass=?");
$st->bindParam(1,$user);
$st->bindParam(2,$pass);
$st->execute();
if($st->rowCount()==1)
{
$_SESSION['admin_user']=$user;
header("Location:admin.php");
}
else
{
$_SESSION['errorMessage']="Incorrect username or password!Please try again.";
echo $_SESSION['errorMessage'];
}
}
else
{
echo "Hey! Don't leave me empty";
}
}
}
?>
index.php
require_once '../cms/user.php';
if(isset($_POST['submit']))
{
$user=$_POST['user'];
$user= mysql_real_escape_string($user);
$pass=$_POST['pass'];
$pass= mysql_real_escape_string($pass);
$object=new user();
$object->login($user,$pass);
}
When I am trying to login with false or correct data, it throws fatel error. Even it was working file yesterday. But what happened today. This is annoying. Please help.
You need return the pdo instance.
class connection{
public function dbConnect()
{
// here, you need to return it
return new PDO("mysql:host=localhost:3306;dbname=education","root","");
}
}