“Powerful database abstraction layer with many features for database schema introspection, schema management and PDO abstraction.”
The following will get you started, these offer the Doctrine\Common and Doctrine\DBAL namespaces.
In the end your structure should look something like that:
includes/
includes/doctrine
includes/doctrine/lib
includes/doctrine/lib/Doctrine
includes/doctrine/lib/Doctrine/Common
includes/doctrine/lib/Doctrine/DBAL
The following will add a class loader, so that all the other classes will be autoloaded.
use Doctrine\Common\ClassLoader; $classLoader->register();
This will setup your first connection to a MySQL database.
$config = new \Doctrine\DBAL\Configuration(); 'dbname' => 'my_db', 'user' => 'my_user', 'password' => 'my_pass', 'host' => 'localhost', 'driver' => 'pdo_mysql', ); $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
This will do a simple first query
$sql = "SELECT * FROM some_data"; $stmt = $conn->query($sql); while ($row = $stmt->fetch()) { echo $row['some_field']; }
DBAL gives us some nice options to prepare queries.
$sql = "SELECT * FROM some_data WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bindValue(1, $id); $stmt->execute();
By using the bindValue the placeholder “?” is replaced. You can also use named parameters :)
$sql = "SELECT * FROM some_data WHERE name = :name OR username = :name"; $stmt = $conn->prepare($sql); $stmt->bindValue("name", $name); $stmt->execute();
More about this in the official documentation.
That was not too difficult ;)
BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More
THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More
2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More
Short look back at 2023 This has been a busy and interesting year. I am… Read More
Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More
Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More