visit
The
Like many other languages, PHP has the ability to take advantage of the power of
But talk, text, in this case, is cheap. So to demonstrate this I’ve created a
In this article, I’m going to highlight, from a high level, some of the fundamental details of using PHP to connect to and communicate with a
Before jumping into the PHP code for the application it’s important to note that it uses a single database called rolodex.
CREATE DATABASE `rolodex`;
The rolodex database contains a single table, contacts, that is used to store basic information.
CREATE TABLE `rolodex`.`contacts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`age` INT(3) NOT NULL,
`email` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
);
The SQL necessary to run the Rolodex application can be found in the
If you don't have an instance of MariaDB up and running yet you can find more information on how to get started in this
To facilitate the use of a MariaDB database within the Rolodex PHP application, I’ve created a new file called
<?php
// Basic connection settings
$databaseHost = '<host_address>';
$databaseUsername = '<user_name>';
$databasePassword = '******';
$databaseName = 'rolodex';
// Connect to the database
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
Within the config.php file I’ve started by defining variables that hold the host address, username, password, and default database that are used to create a new
Using, and reusing, the mysqli
connection within config.php is as easy as including it within a PHP code block on another PHP page.
<?php
// Include the database connection file
include_once("config.php");
...
?>
Then, with an established connection, you have the ability to use a plethora of capabilities from the mysqli extension, including executing queries using
Selecting data
<?php
// Include the database connection file
include_once("config.php");
// Fetch contacts (in descending order)
$result = mysqli_query($mysqli, "SELECT * FROM contacts ORDER BY id DESC");
?>
Selecting contacts using mysqli_query
Or, in the case that you need to handle dynamically inserted parameter values, you can use
Inserting data
$stmt = $mysqli->prepare("INSERT INTO contacts (name,age,email) VALUES(?, ?, ?)");
Inserting contacts using mysqli_prepare
Updating data
$stmt = $mysqli->prepare("UPDATE contacts SET name=?, age=?, email=? WHERE id=?");
$stmt->bind_param("sisi", $name, $age, $email, $id);
$stmt->execute();
Updating contacts using mysqli_prepare
Deleting data
$stmt = $mysqli->prepare("DELETE FROM contacts WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
Deleting contacts using mysqli_prepare
As you can see, getting started with PHP and MariaDB is easy, but we’ve only scratched the surface of what’s possible. If you’d like to see for yourself what else is possible with PHP and MariaDB, start by checking out the full source code for the Rolodex application in the new
And if you’d like to learn even more about what’s possible with MariaDB be sure to check out the
You can also dive even deeper into MariaDB capabilities in the
And, as always, MariaDB is deeply rooted in open-source and would be nothing without their awesome community! If you’d like to help contribute directly to MariaDB you can find them on
Also Published