Please wait

Understanding Databases

Databases play a pivotal role in PHP applications, as they do in many web-based applications.

Databases provide a structured way to store vast amounts of information, from user accounts to posts, comments, transactions, and more. Instead of static HTML content that remains constant, PHP applications can generate content on the fly based on data fetched from databases. For instance, when you visit an e-commerce site and view a product, the details of that product are typically retrieved from a database.

Benefits of a Database

It's not uncommon for developers to use a database with their PHP application. There are many benefits to doing so.

  • PHP can interact with databases to insert, update, delete, and retrieve data, facilitating interactive features like user account management, posting content, and more.
  • As applications grow, the amount of data they manage can increase exponentially. Databases are designed to handle this growth, and PHP applications can scale with them.
  • Databases, when configured properly, can provide secure storage of sensitive information. PHP applications can leverage features like prepared statements to prevent SQL injection attacks and ensure data integrity.
  • Databases enable powerful search functionalities, allowing users to quickly find information. Additionally, data analytics and reports can be generated from stored data to provide insights.

Relational Databases

The question becomes, what database should you use? There are dozens to choose from. One of the most popular types of databases to use is relational databases.

Imagine you have a collection of boxes. Inside each box, you have rows of information cards. Each card in the same box has the same type of information, but the details differ. For example, one box might have cards with names and addresses (let's call this the "Address Box"), while another might have cards with names and favorite foods (the "Food Box").

Now, because both boxes have names on the cards, you can connect or "relate" cards from the "Address Box" to cards in the "Food Box". So if you wanted to find out the favorite food of someone and their address, you could do it by using the name as a common link between the two boxes.

In the world of databases, the boxes are called "tables", and the cards are "rows". The idea of linking information between these tables using a common detail (like the name) is fundamental to a "relational database".

Why use Relational Databases?

They're extremely popular with PHP developers. As we described, relational databases allow data to be organized into different tables, making it easy to manage and find information. Developers can easily fetch specific data from multiple tables. For example, on a website with articles and authors stored separately, one can fetch all articles by a specific author with a single query.

In addition, relational databases have been around for a long time, so they're reliable and well-understood. There's a lot of support, documentation, and tools available. Given the long-standing use of relational databases with PHP, there's a large community and, thus, a plethora of tutorials, forums, and resources available.

Here are some popular relational databases frequently used by PHP developers:

  1. MySQL: One of the most well-known and widely used databases with PHP. It's an open-source database and is often associated with web applications.
  2. MariaDB: A fork of MySQL created by the original developers of MySQL after concerns arose when Oracle acquired MySQL. It's fully compatible with MySQL and offers more features.
  3. PostgreSQL: An open-source database known for its extensibility and SQL compliance. Some developers prefer it over MySQL due to its advanced features.
  4. SQLite: Unlike other databases that are server-based, SQLite is a lightweight, serverless database. It's often used for smaller projects or as an embedded database.
  5. Microsoft SQL Server (MSSQL): While not as commonly associated with PHP as MySQL or PostgreSQL, it's still a robust relational database system used in many enterprise applications.

These are just a few examples, and the best database often depends on the specific needs and constraints of the project. However, MySQL and its fork, MariaDB, are especially popular in the PHP community due to their ease of use, performance, and wide support in hosting environments.

For this book, we'll be using MySQL/MariaDB.

Understanding SQL

If you read the descriptions of each option, you may have noticed that the word SQL comes up a lot. Think of SQL as a special language used to talk to databases. Just like you'd use English or Spanish to communicate with people, you use SQL to ask databases questions or give them instructions.

SQL stands for Structured Query Language. But don't let the fancy name scare you. It's just a way of saying: "a structured way to ask questions (or queries) to a database."

Why do so many databases use it?

Imagine if every country had its own completely unique language with no similarities. It'd be tough to travel or communicate! Similarly, having a common language like SQL for different databases makes it easier for people to learn and use them.

When something becomes popular, and lots of people start using it, it becomes a standard. Over time, SQL proved to be really good at its job, so many databases adopted it.

To sum up, SQL is like the universal language for talking to databases. It's been around for a while, is good at what it does, and that's why so many databases use it!

PHPMyAdmin

Earlier in this book, we showed you how to use XAMPP. Bundled with XAMPP is a program called PHPMyAdmin. PHPMyAdmin is a program for interacting with the database installed with XAMPP.

You can open PHPMyAdmin by clicking on the Admin button in the MySQL row.

Opening PHPMyAdmin with XAMPP
Opening PHPMyAdmin with XAMPP

After doing so, you'll be able to view your database.

PHPMyAdmin
PHPMyAdmin

Before you can start learning SQL, you'll need a database. You can think of a database as a container for your data. Multiple containers can exist on a single machine.

Luckily, PHPMyAdmin allows you to create a database easily. At the top right corner, there's a button called Databases. If you click on it, you'll be taken to a form to create a database. For this example, create a database called example and set the collation to utf8mb4_general_ci.

Creating a Database
Creating a Database

After creating the database, you'll be taken to a page to view your database. At the top of the page, there's a tab called SQL. If you click on it, you'll be taken to a page to write custom queries.

SQL Tab
SQL Tab

Queries can be used to interact with the database, such as creating tables or inserting data. For the rest of this chapter, you should be viewing this page and writing your queries in the box. After doing so, you can press the Go button to execute the query.

Key Takeaways

  • Relational databases store, organize, and manage data for PHP applications, making them essential for dynamic and data-driven websites.
  • PHP interacts with relational databases using SQL (Structured Query Language) to perform operations like inserting, updating, or retrieving data.
  • MySQL and MariaDB are among the most popular relational databases in the PHP ecosystem, but others like PostgreSQL and SQLite are also commonly used.
  • Given the extensive use of relational databases with PHP, there's a wealth of community resources, tutorials, and tools available to assist developers.

Comments

Please read this before commenting