Skip to content
Learn Netverks

Lesson

Step 29/36 81% through track

pdo-intro

PDO introduction

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

An orientation to the PHP track—how the server playground works, core vocabulary, and what you will practice next.

You need a clear map of the PHP track so HTTP, requests, and server execution do not feel like magic.

You will apply PDO introduction in contexts like: Laravel Eloquent under the hood, WordPress plugins, and custom CRUD admin panels.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally). Also read the interview prep blocks.

After HTML fundamentals and basic programming concepts—before or alongside SQL.

PDO (PHP Data Objects) is the modern database abstraction layer for MySQL, PostgreSQL, SQLite, and others. It replaces deprecated mysql_* functions with a consistent API and prepared statement support.

Connection (real project)

$pdo = new PDO(
    'mysql:host=127.0.0.1;dbname=app;charset=utf8mb4',
    'user',
    'pass',
    [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
);

Why PDO

  • Prepared statements reduce SQL injection risk
  • Exceptions on failure (ERRMODE_EXCEPTION)
  • Swappable drivers for different databases

Playground note

This runner has no live database. Practice query structure with mock result arrays; run PDO against local MySQL/MariaDB or SQLite when ready.

Important interview questions and answers

  1. Q: PDO vs mysqli?
    A: PDO supports multiple drivers and a lean OOP API; mysqli is MySQL-specific with async features—PDO is the default recommendation for portable apps.
  2. Q: Why ERRMODE_EXCEPTION?
    A: Failures become catchable exceptions instead of silent false returns.

Self-check

  1. What DSN string component selects the database type?
  2. Why set default fetch mode to associative arrays?

Interview prep

Why PDO ERRMODE_EXCEPTION?

Database failures throw catchable exceptions instead of returning false silently—easier to log and handle in production.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • PDO vs mysqli?
  • DSN parts?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump