DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • PHP vs React
  • Bonsai Checklist: 5 Rules to Make Your Open-Source Project Popular
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Leveraging Open-Source Contributions to Boost Your Freelancing Profile

Trending

  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • Why Your DLP Policies Fall Short the Moment AI Agents Enter the Picture
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  1. DZone
  2. Data Engineering
  3. Databases
  4. Developer Quickstart: PHP and MariaDB

Developer Quickstart: PHP and MariaDB

A short getting started tutorial for PHP and MariaDB

By 
Rob Hedgpeth user avatar
Rob Hedgpeth
·
Jan. 22, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

The PHP programming language has long been considered a fairly light and simple solution for developers to use to create robust and creative web applications. In fact, that’s as true now as it has ever been as past few versions of PHP have made the language more appealing than ever.

Like many other languages, PHP has the ability to take advantage of the power of MariaDB, and it’s actually a pretty simple and straightforward process.

But talk, text in this case, is cheap. So to demonstrate this I’ve created a simple PHP web application called Rolodex to manage contacts.

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 MariaDB database. Everything I’ll be exploring is based on the code for the Rolodex application, and if you’d like to dive into the code you can check it out here.

Preparing the database

Before jumping into the PHP code for the application it’s important to note that it uses a single database called rolodex.

MariaDB SQL
 
CREATE DATABASE `rolodex`;

The rolodex database contains a single table, contacts, that is used to store basic information.

MariaDB SQL
 
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 schema.sql file.

Tip: 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 MariaDB Quickstart Guide.

Configuring the application

To facilitate the use of a MariaDB database within the Rolodex PHP application I’ve created a new file called config.php that contains the configuration settings and database connection object that can be reused across PHP pages. Connecting to and communicating with an underlying MariaDB database is facilitated by the mysqli PHP extension.

PHP
 
<?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 mysqli connection object, which contains a variety of configuration options that you can use to tailor to your environment.

Executing SQL

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
 
<?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 mysqli_query.

Selecting data

PHP
 
<?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 mysqli_prepare.

Inserting data

PHP
 
$stmt = $mysqli->prepare("INSERT INTO contacts (name,age,email) VALUES(?, ?, ?)");

Inserting contacts using mysqli_prepare

Updating data

PHP
 
$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

PHP
 
$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 PHP Quickstart GitHub repository.

Learn more

And if you’d like to learn even more about what’s possible with MariaDB be sure to check out the Developer Hub and the new MariaDB Developer Code Central GitHub organization. There you can find much more completely open-source content just like this spanning a variety of other technologies, use cases and even programming languages.

You can also dive even deeper into MariaDB capabilities in the official MariaDB documentation.

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 GitHub, send feedback directly to their Developer Relations team at [email protected], or join the conversation in the new MariaDB Community Slack!

Happy coding!

PHP MariaDB dev Web application Database connection Open source

Published at DZone with permission of Rob Hedgpeth. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • PHP vs React
  • Bonsai Checklist: 5 Rules to Make Your Open-Source Project Popular
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Leveraging Open-Source Contributions to Boost Your Freelancing Profile

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook