PHP PDO Tutorial: Safe MySQL Connections, SELECT and INSERT
Learn how to connect to MySQL with PHP PDO and run safe SELECT and INSERT queries using prepared statements. Clear examples help you avoid SQL injection and keep your database code clean.


PDO (PHP Data Objects) is a database access layer for PHP. PDO lets PHP work with a range of database drivers, so your code doesn’t depend on a particular database system. PDO ships with drivers for databases such as MySQL, PostgreSQL, Oracle, MS SQL Server and SQLite.
PDO offers a safe way to keep your queries and data secure. For example, PDO supports parameter binding to prevent vulnerabilities such as SQL injection. Binding sanitizes the data before it is added to SQL queries, so data supplied by users can’t inject harmful code into the SQL statement.
PDO also has speed advantages. For example, PDO doesn’t keep a database connection open all the time; it opens and closes the connection only when needed. This reduces the load on the database server and improves speed.
In the example below, we connect to a MySQL database using PDO and run the query “SELECT * FROM users”.
<?php
// Define the database connection
$dsn = 'mysql:dbname=veritabani_adi;host=127.0.0.1';
$kullanici_adi = 'kullanici_adi';
$sifre = 'sifre';
try {
// Create the PDO object
$db = new PDO($dsn, $kullanici_adi, $sifre);
} catch (PDOException $e) {
echo 'Bağlantı başarısız: ' . $e->getMessage();
exit;
}
// Prepare the query
$sorgu = $db->prepare('SELECT * FROM users');
// Execute the query
$sorgu->execute();
// Fetch the results
$kullanicilar = $sorgu->fetchAll();
// Print the results to the screen
foreach ($kullanicilar as $kullanici) {
echo $kullanici['isim'] . '<br>';
}
In this example, we connect to a MySQL database using PDO and run the query “SELECT * FROM users”. First we define the database connection with the dsn, kullanici_adi and sifre variables. When creating the PDO object, we use the dsn, kullanici_adi and sifre variables. If the connection fails, we print the error message to the screen and stop execution. After that we prepare and execute the query, fetch the results with the fetchAll() function and print them to the screen.
In the example below, let’s look at how an INSERT operation is done with a simple snippet of code.
<?php
// Add a new user to the database
$isim = "John Doe";
$email = "johndoe@example.com";
$sifre = password_hash("password", PASSWORD_DEFAULT);
// Prepare the query using parameter binding
$eklemeSorgusu = $db->prepare("INSERT INTO users (isim, email, sifre) VALUES (:isim, :email, :sifre)");
// Bind the variables to the query
$eklemeSorgusu->bindParam(':isim', $isim);
$eklemeSorgusu->bindParam(':email', $email);
$eklemeSorgusu->bindParam(':sifre', $sifre);
// Execute the query
$eklemeSorgusu->execute();
// Get the number of affected rows
$satirSayisi = $eklemeSorgusu->rowCount();
if ($satirSayisi > 0) {
echo "Yeni kullanıcı başarıyla eklendi.";
} else {
echo "Kullanıcı ekleme işlemi başarısız oldu.";
}
In this code block, we add a new user to the database using PDO. First we prepare the insert query and add the variables to the query using parameter binding. Then we execute the query and get the number of affected rows to check whether the insert succeeded. In this example the isim, email and sifre variables are used, and they are matched to the columns in the database. Also keep in mind that passwords are stored hashed.
Share it if it helped


