vadnica-logo

Connecting to a Database via PDO

PDO (PHP Data Objects) is an interface for accessing various databases. Its greatest advantage is that it enables secure work with prepared statements and easy switching between different database types (MySQL, PostgreSQL, SQLite).

PHP MySQLi
RESULT
NOTE: This simulator is intended exclusively for learning the basic PHP MySQLi syntax. To ensure speed and security, the simulator runs pre-defined SQL commands in a controlled environment in the background. If you wish to modify the code or build your own applications, we recommend doing so in your local development environment (e.g., XAMPP) using a real database.

Overview of Common PDO Operations

Below is a list of key commands in the PDO library. You will notice that the SQL logic remains the same as in MySQLi, but the way PHP "communicates" with the database is more modern and object-oriented.

Command / Method How it works in PDO Examples
PDO CONNECT Establishing a connection using a DSN (Data Source Name). See Example
CREATE DATABASE Creates a new database using the exec() method. See Example
CREATE TABLE Creates a new table with defined columns. See Example
INSERT INTO Inserts a record using either exec() or prepare() methods. See Example
LAST INSERT ID Retrieves the ID of the last inserted record using lastInsertId(). See Example
PREPARED Secure database interaction using named parameters (:name). See Example
SELECT / FETCH Fetches data and stores it into an Array. See Example
WHERE Filtering data based on specific conditions. See Example
ORDER BY Sorting results in a specific order. See Example
DELETE Deleting specific records from a table. See Example
UPDATE Updating existing rows within a table. See Example
TRANSACTIONS Advanced operations using beginTransaction, commit, and rollback. See Example

Thank you for visiting! Adding privacy policy.

© 2024 All rights reserved.

Vam je koda pomagala? Če želite podpreti moj trud pri pripravi vodičev in vzdrževanju strani, mi lahko namenite donacijo za kavo.