Simple PDO Setup and PDO Usage

Mysql_* functions have started to reach the end of their life, which is why PHP has dropped support for the mysql_* commands, and PHP developers no longer recommend using them either. So we will cover PDO, which is more secure and performs better.
- First of All, What Do We Need to Do to Use PDO?
You need to enable the necessary driver settings
- Debian GNU/Linux Installation
Since it is a module that comes with PHP 5, updating the version will be enough.
# apt-get install php5-common libapache2-mod-php5 php5-cli
Entering this command gets the required packages installed,
# /etc/init.d/apache2 stop # /etc/init.d/apache2 start
and then entering these commands stops the Apache service and starts it again.
- Windows Installation
All of the main drivers come with PHP and need to be enabled in the php.ini file:
Whichever database driver you are using, remove the semicolon ( ; ) in front of the driver name.
extension=php_pdo.dll ; extension=php_pdo_firebird.dll ; extension=php_pdo_informix.dll ; extension=php_pdo_mssql.dll extension=php_pdo_mysql.dll ; extension=php_pdo_oci.dll ; extension=php_pdo_oci8.dll ; extension=php_pdo_odbc.dll ; extension=php_pdo_pgsql.dll ; extension=php_pdo_sqlite.dll
Since we will be using MySQL, in the php.ini file we enable the
extension=php_pdo.dll and extension=php_pdo_mysql.dll
directives.
And with that, we have done what is needed to be able to use PDO.
- Now, How Is PDO Used?
- PDO Database Connection
try {
$db = new PDO("mysql:host=localhost;dbname=mtlive,charset=utf8", "root", "123456");
} catch ( PDOException $e ){
echo 'Error: '.$e->getMessage();
}
Here we used the try catch structure so that if we run into any error we can see clearer and more sensible error messages. And by using charset we set the character set to UTF-8 .
- PDO Select Operation (Listing Data with a Loop)
$sql = $db->prepare("SELECT * FROM icerik");
$sql->execute();
while($row=$sql->fetch(PDO::FETCH_ASSOC))
{
echo $row['icerik_baslik']."
";
}
Here, from the table named icerik we fetched the data in the icerik_baslik column and printed it to the screen.
- PDO Select Operation (Fetching a Single Record)
$id = $db->quote($_GET['id']);
$query = $db->query("SELECT * FROM icerik WHERE id= ?")->fetch(PDO::FETCH_ASSOC);
$query ->bindParam(1, $id );
if ( $query ){
echo $query['icerik_baslik'];
}
Here, we take the $_GET request's id value, match it against the id value in the icerik table, and fetch the data in its icerik_baslik column.
- PDO insert Operation (Adding Data)
$baslik = "Content Title";
$icerik = "Content Description";
$isim= "Mücahit TOPAL";
$sql = $db->prepare('INSERT INTO icerik(baslik,icerik_baslik,isim) VALUES (?,?,?)');
$ekle = $sql->execute(array(
$baslik,
$icerik,
$isim
));
if ( $insert )
{
$last_id = mysql_insert_id();
echo "Record added successfully!";
}
The values coming from the $baslik , $icerik and $isim variables have now been saved into the icerik_baslik , icerik , isim columns of the icerik table.
- PDO delete Operation (Deleting Data in Bulk)
$delete = $db->exec("DELETE FROM icerik");
echo 'Total of '.$delete.' records deleted!';
We have now deleted all the records in the icerik table.
- PDO delete Operation (Deleting a Single Record)
$query = $db->prepare("DELETE FROM icerik WHERE id = :id");
$delete = $query->execute(array(
"id" => $_GET['id']
));
Based on the $_GET request's id value, we have now deleted from the icerik table the record that has that id value.
- PDO update Operation (Updating Data)
$baslik = "Content Title";
$icerik = "Content Description";
$isim= "Mücahit TOPAL";
$id = $_GET['id'];
$query = $db->prepare("UPDATE icerik SET icerik_baslik=? , icerik=? ,isim=? WHERE id=?");
$update = $query->execute(array(
$baslik,
$icerik,
$isim,
$id
));
if ( $update ){
echo "Record updated successfully!";
}
Based on the $_GET request's id value, we have now updated the matching row in the icerik table, writing the values from the $baslik , $icerik and $isim variables into the corresponding icerik_baslik , icerik , isim columns, as an update.
- Closing the PDO Database Connection
$db = null;
And so we have gone through PDO usage with simple examples.
Hoping you get to use it in great projects. Take care...
PHP,PDO,Select,Insert,Update,Delete,Connect,Seçim,Ekleme,Silme,Güncelleme,Bağlantı