Databases with PHP

Содержание
Introduction
Ways to work with databases in PHP
mysqli
mysqli OOP example
mysqli procedural example
PDO

Introduction

More on relational databases in the articles:

Ways to work with databases in PHP

mysqli

MySQLi is a driver for MySQL databases. Used instead of deprecated mysql since PHP 5.

i stands for improved

Examples of connecting to a database with mysqli

OOP

<$php $servername = "localhost"; $username = "andrei"; $password = "secret"; // Open a MySQL connection $conn = new mysqli($servername, $username, $password); // Check the connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Succesfully connected to MySQL!"; // Create a database $sql = "CREATE DATABASE newDB"; if ($conn->query($sql) === TRUE) { echo "Database created successfully!"; } else { echo "Error creating database: " . $conn->error; } ?>

Procedural

<$php $srvname = "localhost"; $usrname = "andrei"; $password = "secret"; // Open a MySQL connection $conn = mysqli_connect($srvname, $usrname, $password); // Check the connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Succesfully connected to MySQL!"; ?>

PDO

PHP Data Objects. Supports 12+ different databases.

If your project may switch relational databases - using PDO makes sense.

Example of creating a table

CREATE TABLE `sample`.`Person` ( `Id` INT NOT NULL AUTO_INCREMENT , `Firstname` VARCHAR(50) NOT NULL , `Lastname` VARCHAR(50) NOT NULL , `email` VARCHAR(255) NOT NULL , PRIMARY KEY (`Id`) ) ENGINE = InnoDB;

Article author: Andrei Olegovich

Похожие статьи
Development with PHP
Arrays in PHP
Date and Time in PHP - basics
How to display time of multiple timezones in PHP
How to add variable to url in PHP
json_decode
How to get screen size with PHP
Call function from another file
Premature end of chunk coded message body: closing chunk expected
Generate unique random numbers with PHP
Check your HTTP_USER_AGENT
Compare two dates
OOP
Namespaces in PHP
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8
PHP linters we use
PHP errors: case studies
card_from_db(): banner card from a DB row
PHP versions: history in brief
Creating rules in .htaccess
require, require_once, include, include_once
Flat pair arrays: three pitfalls from the Related articles block
Deduplication in PHP
PHP course: zero to hero in 50 lessons

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: