PHP MySQL Connect
There are three types of methods in PHP to connect MySQL database through backend:
- MySQL
- MySQLi (the "i" stands for improved)
- PDO (PHP Data Objects)
Note: MySQL This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Difference Between PDO and MySQLi
SrN | PDO | MySQLi |
1 | PDO will work on 12 different database systems | MySQLi will only work with MySQL databases |
2 | 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 | MySQLi, you will need to rewrite the entire code - queries included |
3 | PDO is object-oriented | MySQLi support Object-oriented and procedural both |
4 | Support Prepared Statements | Support Prepared Statements |
What is Prepared Statements ?
Prepared Statements protect from SQL injection, and are very important for web application security.
MySQLi
1- MySQLi Procedure based connection
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo 'Connected...';
}
// Connection closed.
mysqli_close($conn);
?>
Note: In this example we have selected database, this is only stablish the connection.
2- MySQLi Object-oriented based connection
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
PDO
<?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();
}
?>
Related post
EknowledgePoint
Increase Your Knowledge with usEknowledgePoint is an innovative online learning platform that aims to democratize education by providing a vast array of courses and educational content to learners of all ages and backgrounds. It offers a diverse range of subjects, from academic topics to practical skills, making it a one-stop destination for anyone looking to expand their knowledge base.
5 comments
Knowledge is the fuel that powers our journey through life. It's platforms like EknowledgePoint that transform information into enlightenment, curiosity into competence, and dreams into reality. Embrace the opportunity to learn, and you'll find that the doors of possibility swing wide open. 🚀💡 #EknowledgePoint.
Thank you for your kind words! At EknowledgePoint, we are dedicated to empowering individuals on their learning journey. Your enthusiasm for knowledge and personal growth inspires us to keep providing the best educational resources and experiences. Let's continue to explore and learn together!.
Thanks For Your Support.
"I'm constantly amazed by the quality of instructors on EknowledgePoint. Learning from experts in their fields is both enlightening and empowering. 🌟👨🏫 #EknowledgePoint.
EknowledgePoint isn't just a platform; it's a community of learners. The support and camaraderie here are truly inspiring. 🤝🌍 #EknowledgePoint.