PHP Database MySQLi Procedural Select Statement

MySQL Improved Extension In PHP

The PHP mysqli extension supports persistent connections when using either MySQL Native Driver or MySQL Client Library.

PHP mysqli extension procedural interface is similar to the old deprecated mysql extension and is similar to other programming languages.

Some mysqli functions require a connection handle as the first argument. This tutorial uses MariaDB 10.11.8 and PHP 8.3.10 on Fedora Linux 40.

Procedural Interface

Sample mysqli functions
Name Description Example
mysqli_connect Create database connection mysqli_connect($dbname, $dbuser, $dbpass)
mysqli_real_escape_string Escapes special characters in a string mysqli_real_escape_string(mysqli $mysql, string $string): string
mysqli_query Perform database query mysqli_query(mysqli $mysql, string $query, int $result_mode = MYSQLI_STORE_RESULT): mysqli_result|bool
mysqli_num_rows Gets the number of rows in the result set mysqli_num_rows(mysqli_result $result): int|string
mysqli_fetch_assoc Fetch the next row of a result set as an associative array mysqli_fetch_assoc(mysqli_result $result): array|null|false
Name Description Example

PHP MySQLi Procedural Select Data

<?php
$conn = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
if (!$conn) {
	die("DB Connection Error " . mysqli_connect_error());
}
$result = mysqli_query($conn, "SELECT sid, name, age FROM Students");
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
	echo "Student ID: {$row["sid"]}, Name: {$row["name"]}, Age {$row["age"]}\n"; 
}
?>

SQL Injection Security Warning

Variable input must be sanitized, validated, properly formatted and escaped with the mysqli_real_escape_string() function. For this tutorial, the variable input is known and trusted and security is not concerning.

PHP MySQLi Procedural Select Data Escape String

<?php
$conn = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
if (!$conn) {
	die("DB Connection Error " . mysqli_connect_error());
}
$result = mysqli_query($conn, sprintf("SELECT sid, name, age FROM Students WHERE name = '%s'", mysqli_real_escape_string($conn, "D'Arcy")));
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
	echo "Student ID: {$row["sid"]}, Name: {$row["name"]}, Age {$row["age"]}\n"; 
}
?>

MySQL Database Table Structure
PhpMyAdmin Displaying Mysql Database Students Structure

MySQL Database Table Data
PhpMyAdmin Displaying Mysql Database Students Data

PHP MySQLi Procedural Select
Geany IDE Displayed PHP MySQLi Procedural Select Code

PHP MySQLi Procedural Select Result
Geany IDE Displayed PHP MySQLi Procedural Select Result


Usage

You can use any IDE or text editor and the command line to compile and execute PHP code. In this tutorial, the OjamboShop.com Learning PHP Course completion inspired the database connection.

Open Source

PHP is licensed under the PHP License v3.01. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Courses are optimized for your web browser on any device.

OjamboShop.com Learning PHP Course
OjamboShop.com Learning PHP Interactive Online Course

OjamboShop.com Learning Python Course
OjamboShop.com Learning Python Interactive Online Course

Limited Time Offer:

OjamboShop.com is offering 20% off coupon code SCHOOL for Learning Python Course or for Learning PHP Course until End Day 2024.

Learn Programming Ebooks:

Ebooks can be downloaded to your reader of choice.

OjamboShop.com Learning PHP Ebook
OjamboShop.com Learning PHP Ebook With Sample Page

OjamboShop.com Learning Python Ebook
OjamboShop.com Learning Python Ebook Cover Page

Conclusion:

PHP makes it easy to perform database connections using the MySQL Connector. The mysqli extension was utilized via the procedural method to select database data.

Take this opportunity to learn the PHP or Python programming language by making a one-time purchase at Learning PHP Course or Learning Python Course. A web browser is the only thing needed to learn PHP or Python in 2024 at your leisure. All the developer tools are provided right in your web browser.

If you prefer to download ebook versions for your reader then you may purchase at Learning PHP Ebook or Learning Python Ebook

References:

Leave a Reply

Your email address will not be published. Required fields are marked *