I am still learning mostly from books I buy, but today I leart that my book is old even though I bought it this year concerning programming in PHP. Now I know that mysql_* commands in PHP are deprecated and should be replaced with more secure and stable prepared statements and PDO. So I put myself to rewrite all my web according to it and maybe I will need some advices from you how to do it properly and working from you all more experienced guys :)
So I will start my rewrite with only main part (connect to db and choosing DB) in here (the rest I can do on my own with google and manuals). I will write here my old script and ask you if I am doing everything right and not missing anything and I hope this could be some good manual/answer for other people as well. So lets start.
So in config I have something like this:
$db = new dbConn('127.0.0.1', 'root', 'pass', 'people', 'animals');
Which should be like this:
$db = new PDO('mysql:host=127.0.0.1;dbname=people;charset=UTF-8', 'root', 'pass');
Right? But when I need to choose database later should i do it without dbname=people;
? But how to choose database later?
Here is my one and only script to rewrite which is basic in most web projects and I hope it will bring not only me some understanding how new PDO system really works:
class dbConn
{
public function __construct($server, $user, $pass, $db_people, $db_animals)
{
if (!empty($server) && !empty($user) && !empty($pass) && !empty($db_people) && !empty($db_animals))
{
$this->server = $server;
$this->user = $user;
$this->pass = $pass;
$this->db_people = $db_people;
$this->db_animals = $db_animals;
$this->connect();
}
else
{
die("Set up connection to db");
}
}
public function connect()
{
$this->conn = mysql_connect($this->server, $this->user, $this->pass) or die ('cannot connect to MySQL');
}
public function selectDb($database)
{
switch($database)
{
case 'people':
mysql_select_db($this->db_people, $this->conn) or die ('cannot connect to database '.$this->db_people.'.');
mysql_query("SET NAMES 'utf8'");
break;
case 'animals':
mysql_select_db($this->db_animals, $this->conn) or die ('cannot connect to database '.$this->db_animals.'.');
mysql_query("SET NAMES 'utf8'");
}
}
public function __destruct()
{
if (!empty($this->conn))
{
mysql_close($this->conn);
}
}
}
So from what I know from Google and Wiki - functions like public function __construct
and public function __destruct()
should not be needed anymore, right? The same with functions like public function connect()
SO only whats left is public function selectDb($database)
but i have no idea how to do this correctly without damading all connection to database. Because in rest of my code (not mentioned here) I can easily choose database by this code: $this->db->selectDb("people");
But with prepared statements I do not know if this is even possible in easy way. I hope some advices around this from you will help me and other users understand this new code better. Other parts in code you may have are eplained in this PDO Tutorial for MySQL Developers. Thank you.