If you need a short answer, it would be "Whatever you like".
Both MySQLi and PDO have their advantages:
PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases.
So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.
Both are object-oriented, but MySQLi also offers a procedural API.
Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Arvind Dubey is founder of Developer Blog. He is an engineer as per his education and blogger by his profession. Before becoming a professional blogger. he decied to be his own boss and started blogging part time. His passion is blogging and under shoutmeloud, he blogs on topics like blogging tips, core php, mysql, javascript, WordPress, Web tools, SEO and so on
1406, Chember Block, Block D, Sector 14, Rohini, Delhi, 110085
Email: info@developerblog.in