PHP.net has officially announce the extension mysql being deprecated if you viste any mysql function description in the php.net for examble http://www.php.net/manual/en/function.mysql-close.php you will get the following notice the altenatives are MySQLi or PDO_MySQL Well I would like to know which of the both (PDO or MySQLi) should I pick to continue my learning progress, taking in mind I have no experience or interest in working on any databases except MySQL, and that I am a newbie. i just want to know which one of the is better and more portable and easy to learn best regards
I prefer PDO to mysqli. If you're just looking for the quickest replacement, mysqli will be a little easier to transition to. PDO is more portable and supports other RDBMS. It also offers quite a bit more features.
Whenever I come across anybody needing help and I see they're using the old style query type I always urge them to switch to PDO. Well I guess they'd better learn quick! Its not actually that hard to pick up, I found it a little niggly at first but its so much easier and nicer to know you have that added layer of security... for the time being.
I've never learnt MySQLi so I can't really comment other than it is meant to be similar to the old MySQL. PDO is fantastic though and its also not entirely dissimilar either. Here's an example: dbconnect.php //Set details $serverHost = "localhost"; $dbName = "myDB"; $userName = "myUser"; $passWord = "myPass"; //Declare new PDO using above credentials $dbconn = new PDO('mysql:host=$serverHost;dbname=$dbName','$userName','$passWord'); //Make sure all is working try { $dbPDO = new PDO('mysql:host='.$serverHost.';dbname='.$dbName, $userName, $passWord); $dbPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } //Check for errors catch (PDOException $e) { echo "Error!: " . $e->getMessage() . " "; die(); } PHP: insertexample.php // insert info to the db $sth = $dbconn->prepare(" INSERT INTO myTable (field1, field2, field3, field4) VALUES (:item1,:item2,:item3,:item4) "); //This is the cool bit, because the items are binded below, PDO AUTOMATICALLY prevents SQL injections making it a heck of a lot safer than the old style $params = array( item1=> $_POST[item1], item2=> $_POST[item2], item3=> $_POST[item3], item4=> $_POST[item4] ); //All set now execute! $sth->execute($params); PHP: selectqueryexample.php $sth = $dbconn->prepare(" SELECT item1, item2, item3, item4 FROM table2 WHERE (item1 = :item1) AND (item2 = :item2) "); $params = array("item1" => $_POST[item1], "item2" => $_POST[item2]); $sth->execute($params); $row = $sth->fetch(PDO::FETCH_ASSOC); echo "$row[item1]"; PHP: