Courses/PHP/Database Integration

    Lesson 12 โ€ข Expert

    Database Integration (MySQL) ๐Ÿ—„๏ธ

    Connect PHP to MySQL with PDO โ€” perform CRUD operations, use prepared statements, transactions, and protect against SQL injection.

    What You'll Learn in This Lesson

    • โ€ข Connecting to MySQL with PDO (recommended) and MySQLi
    • โ€ข CRUD: Create, Read, Update, Delete operations
    • โ€ข Prepared statements to prevent SQL injection
    • โ€ข Transactions for atomic multi-query operations
    • โ€ข Fetch modes and best practices for production

    Try It: CRUD Operations

    Insert, select, update, and delete data with PDO prepared statements

    Try it Yourself ยป
    JavaScript
    // PHP + MySQL CRUD Operations (simulated in JavaScript)
    console.log("=== Connecting to MySQL ===");
    console.log();
    console.log("Two options: MySQLi or PDO");
    console.log();
    console.log("// PDO (recommended โ€” works with any database)");
    console.log("$pdo = new PDO(");
    console.log("    'mysql:host=localhost;dbname=my_app;charset=utf8mb4',");
    console.log("    'username', 'password',");
    console.log("    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]");
    console.log(");");
    console.log();
    
    // Simulate 
    ...

    Try It: Security & Transactions

    Prevent SQL injection, use named parameters, transactions, and compare PDO vs MySQLi

    Try it Yourself ยป
    JavaScript
    // Database Security & Best Practices
    console.log("=== SQL Injection: The #1 Vulnerability ===");
    console.log();
    
    // BAD example
    console.log("โŒ NEVER DO THIS:");
    console.log('  $sql = "SELECT * FROM users WHERE name = \'$name\'";');
    console.log();
    console.log("  If $name = \' OR 1=1 --");
    console.log("  Query becomes:");
    console.log("  SELECT * FROM users WHERE name = '' OR 1=1 --'");
    console.log("  โ†’ Returns ALL users! ๐Ÿ’€");
    console.log();
    
    // GOOD example
    console.log("โœ… ALWAYS USE PREPARED STA
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Concatenating user input into SQL โ€” this is the #1 cause of data breaches. ALWAYS use prepared statements with ? or :name placeholders.
    โš ๏ธ
    Not setting error mode โ€” without ERRMODE_EXCEPTION, PDO silently fails. Always set it on connection.
    โš ๏ธ
    Exposing database errors to users โ€” catch exceptions and show generic messages. Log the real error server-side.
    ๐Ÿ’ก
    Pro Tip: Use PDO over MySQLi for new projects. PDO supports 12+ databases with the same API โ€” switch from MySQL to PostgreSQL without rewriting queries.

    ๐Ÿ“‹ Quick Reference โ€” PDO

    MethodPurpose
    prepare() + execute()Safe parameterized queries
    fetch(FETCH_ASSOC)Get one row as array
    fetchAll(FETCH_ASSOC)Get all rows as array
    beginTransaction()Start a transaction
    commit() / rollBack()Finish or undo transaction
    lastInsertId()Get ID of last inserted row

    ๐ŸŽ‰ Lesson Complete!

    You can now connect PHP to a database securely! Next, learn comprehensive security best practices to protect your web application.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service