I am new to PHP and I am trying to develop a login system for my website. I have managed to successfully login to my website but now I am trying to tidy my code up and place the database connection code in one class and have other php classes access the one database connection class. At the moment, I don't seem to be able to make a connection to my database. I have tried to find the errors by adding in the relevant error reporting code but nothing is output.
I would appreciate it if someone could look over my code and see where I am going wrong!
Here is my php class which checks the username and password - checklogin.php
<?php
require_once(__DIR__. "/DBConnection.php");
class checklogin {
public $count=0;
public function checklogin() {
ob_start();
$db = DBConnection()->get_connection();
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$encrypt_password=md5($mypassword);
$sql="SELECT id FROM members WHERE username='$myusername' and password='$encrypt_password'";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
$this->count=mysql_num_rows($result);
if($this->count==1){
session_start();
$_SESSION['ID']=$row[0];
header("location:login_success.php");
} else {
$this->error();
}
ob_end_flush();
}
}
?>
Class which I am trying to implement the database connection and set up an instance - DBConnection.php
<?php
class DBConnection{
private static $instance;
private function __construct(){
$host=".....";
$username="....";
$password=".....";
$db_name=".....";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
}
public static function get_connection(){
if(empty (self::$instance)) self::$instance = new DBConnection;
return self::$instance;
}
}
?>