Skip to content

Latest commit

 

History

History
228 lines (167 loc) · 7.45 KB

PDO Lesson-1.md

File metadata and controls

228 lines (167 loc) · 7.45 KB

Sure! Let's dive deeper into PDO, focusing on bindParam(), and go through the theory and code examples step by step, starting from the basics of PDO and working up to bindParam().

PDO Basics:

  • PDO (PHP Data Objects) is a database access abstraction layer that allows you to interact with databases in a secure, consistent way, supporting multiple database types (MySQL, PostgreSQL, SQLite, etc.).
  • It provides prepared statements that help prevent SQL injection and enables secure and efficient execution of queries.

PDO Connection: Before using PDO to interact with a database, you need to establish a connection. Here's how to connect to a MySQL database:

<?php
$host = 'localhost';
$db_name = 'test_db'; // Replace with your database name
$username = 'root'; // Replace with your database username
$password = ''; // Replace with your database password

try {
    // Create a PDO instance (connect to the database)
    $pdo = new PDO("mysql:host=$host;dbname=$db_name", $username, $password);
    
    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "Connected successfully!";
} catch (PDOException $e) {
    // Handle connection errors
    echo "Connection failed: " . $e->getMessage();
}
?>

In this connection code:

  • PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION: This makes PDO throw exceptions on errors, which is essential for debugging.

Why Use Prepared Statements?

Prepared statements allow for parameterized queries. This means the query is sent to the database first with placeholders, and later, the actual values are passed in. This method is safe because it prevents SQL injection attacks.

Basic PDO Operations: Let's cover the basic operations that you can perform with PDO: Insert, Select, Update, and Delete.


  1. Insert Data (Create) Here's how you can insert data securely using prepared statements:
<?php
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)"; // SQL query with placeholders
$stmt = $pdo->prepare($sql); // Prepare the query

// Bind values to the placeholders
$stmt->bindParam(':name', $name, PDO::PARAM_STR);  // Bind 'name' as string
$stmt->bindParam(':email', $email, PDO::PARAM_STR);  // Bind 'email' as string

// Assign values to variables
$name = "John Doe";
$email = "[email protected]";

// Execute the statement
$stmt->execute();

echo "User created successfully!";
?>

In the code above:

  • bindParam() binds the $name and $email variables to the placeholders :name and :email respectively.
  • Security: The values are not inserted directly into the SQL query, preventing SQL injection.

  1. Fetch Data (Read) Next, let’s fetch data from the database:
<?php
$sql = "SELECT * FROM users"; // Simple SELECT query
$stmt = $pdo->query($sql); // Execute the query

// Fetch the results
$users = $stmt->fetchAll(PDO::FETCH_ASSOC); // Fetch all rows as an associative array

foreach ($users as $user) {
    echo "Name: " . $user['name'] . " - Email: " . $user['email'] . "<br>";
}
?>

Here:

  • We use fetchAll(PDO::FETCH_ASSOC) to retrieve all rows as associative arrays, so you can easily access the fields by name.
  • Dynamic fetching: In case of large datasets, you can use fetch() to get one row at a time.

  1. Update Data (Update) Now, let’s update data in the database:
<?php
$sql = "UPDATE users SET email = :email WHERE id = :id"; // SQL query with placeholders
$stmt = $pdo->prepare($sql);

// Bind values to placeholders
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Assign values to variables
$email = "[email protected]";
$id = 1;  // Assuming we want to update the user with id = 1

// Execute the statement
$stmt->execute();

echo "User updated successfully!";
?>
  • Prepared Statement: We bind the email and id values to the placeholders :email and :id.
  • This helps ensure that the SQL query is executed safely without direct user input being passed into the query.

  1. Delete Data (Delete) Next, let’s delete a record from the database:
<?php
$sql = "DELETE FROM users WHERE id = :id"; // SQL query with placeholders
$stmt = $pdo->prepare($sql);

// Bind value to the placeholder
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Assign value to id
$id = 1; // Assuming we want to delete the user with id = 1

// Execute the statement
$stmt->execute();

echo "User deleted successfully!";
?>

What is bindParam()?

bindParam() is used to bind a PHP variable to a named or positional placeholder in a prepared statement. The key difference is that bindParam() binds by reference, meaning any changes to the variable after it has been bound will reflect in the executed statement.


Example of bindParam() Working with Loops One of the use cases of bindParam() is binding inside a loop. Here’s an example:

<?php
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);

// Array of user data
$users = [
    ['name' => 'Alice', 'email' => '[email protected]'],
    ['name' => 'Bob', 'email' => '[email protected]'],
    ['name' => 'Charlie', 'email' => '[email protected]'],
];

// Loop through each user and insert
foreach ($users as $user) {
    $stmt->bindParam(':name', $user['name'], PDO::PARAM_STR);
    $stmt->bindParam(':email', $user['email'], PDO::PARAM_STR);
    $stmt->execute();
    echo "Inserted: " . $user['name'] . "<br>";
}
?>
  • Explanation: Each time through the loop, we bind the name and email values dynamically to the SQL placeholders.
  • Benefit: This ensures we insert each user securely into the database without direct string concatenation in SQL.

bindParam() vs bindValue()

  • bindParam(): Binds a variable to a placeholder by reference. If the variable changes after binding, the new value will be used when the statement is executed.
  • bindValue(): Binds a value (not a variable) to a placeholder. The value is used at the time of binding and doesn’t change even if the variable is updated after the bind.

Code Example Showing the Difference

<?php
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);

// Using bindParam (by reference)
$name = "Alice";
$email = "[email protected]";
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$name = "Updated Alice"; // Changing variable after binding
$stmt->execute(); // Inserts 'Updated Alice'

// Using bindValue (by value)
$name = "Bob";
$email = "[email protected]";
$stmt->bindValue(':name', $name, PDO::PARAM_STR);
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$name = "Updated Bob"; // Changing variable after binding
$stmt->execute(); // Inserts 'Bob', not 'Updated Bob'
?>
  • bindParam() inserts "Updated Alice", as the variable $name was changed before execution.
  • bindValue() inserts "Bob", because the value was bound at the time of binding and doesn't change.

Summary

  • PDO provides a secure and efficient way to interact with databases.
  • Prepared Statements are the core of PDO, ensuring safety and preventing SQL injection.
  • bindParam() binds PHP variables to placeholders in SQL queries by reference, allowing for dynamic, secure queries.
  • bindValue() binds values to placeholders, capturing the current value at the time of binding.

This should give you a solid understanding of PDO and how to use bindParam() effectively in your applications. Let me know if you need any more details!