hello, i was wondering if you could tell me what would you diffent/better on the following code. I am trying to become a better developer and i would like to learn what the best PHP pratice is. I remove the comments to make it a bit cleaner
//THIS IS THE CLASS I USE TO CONNECT TO THE DB - I USE PDO
class db
{
private static $instance = NULL;
private function __construct()
{
}
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new PDO("mysql:host=localhost;dbname=DB", '', '');;
self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __clone()
{
}
} /*** end of class ***/
//ONE CLASE AS EXPAMPLE
class Contact
{
function __construct()
{
}
//LOGIN FUNCTION
public function login()
{
$sql = " SELECT *
FROM contacts
WHERE username = :username AND password = :password
LIMIT 1;
";
try
{
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':username' , $this->username);
$rs->bindParam(':password' , $this->password);
$rs->execute();
if($rs->rowCount() > 0 )
{
//USER EXIST
$result = $rs->fetch(PDO::FETCH_ASSOC);
$this->login = 1;
@session_start();
$_SESSION["login"]= $this->login ;
foreach($result as $key => $value)
{
$_SESSION[$key] = $value;
}
header('Location: home/');
}else{
/* user not exist*/
$this->error = 'Username/Password not valid. Try again';
return false;
}
} catch (PDOException $e) {
print $e->getMessage();
}
}
//GET ONE RECOROD
public function getRecord($var)
{
try
{
$sql = "
SELECT `contacts`.*
FROM `contacts`
WHERE `contacts`.`contact_id` = :id
LIMIT 1;
";
$rs = db::getInstance()->prepare($sql);
$rs->bindParam(':id' , $var);
$rs->execute();
if($rs->rowCount() > 0 )
{
$result = $rs->fetch(PDO::FETCH_ASSOC);
foreach($result as $key => $value)
{
$this->$key = $value;
}
}
}
catch (PDOException $e)
{
print $e->getMessage();
}
}
// GET ALL RECORDS
public function getRecords()
{
$sql = "
SELECT `contacts`.*
FROM `contacts`
";
try
{
$sth = db::getInstance()->prepare($sql);
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC))
{
$result[] = $row;
}
return $result;
}
catch (PDOException $e)
{
print $e->getMessage();
}
}
// INSERT A NEW CONTACT
public function insert()
{
$sql = "INSERT INTO contacts
(username , password ,name)
VALUES
(:username ,:password ,:name) ;
";
try
{
$sth = db::getInstance()->prepare($sql);
$sth->bindParam(':username' , $this->username );
$sth->bindParam(':password' , $this->password );
$sth->bindParam(':name' , $this->name );
$sth->execute();
return db::getInstance()->lastInsertId();
}
catch (PDOException $e)
{
print $e->getMessage();
}
}
//UPDATE CONTACTS
public function save($var)
{
$sql = "
UPDATE contacts SET
username = :username
,password = :password
,name = :name
WHERE contact_id = :id
";
try
{
$sth = db::getInstance()->prepare($sql);
$sth->bindParam(':id' , $var, PDO::PARAM_INT);
$sth->bindParam(':username' , $this->username);
$sth->bindParam(':password' , $this->password);
$sth->bindParam(':name' , $this->name);
$sth->execute();
}
catch (PDOException $e)
{
print $e->getMessage();
}
}
} //end class
Thanks in advance!