PHP MySQL is where your data finally stops disappearing. Everything so far lived only while the script ran โ the moment it finished, the values were gone. A database keeps them.
What a database actually is
Think of a spreadsheet. Each sheet is a table. Each column is a field with a fixed type. Each row is one record โ one user, one product, one comment.
MySQL is the program that stores those tables and answers questions about them. PHP is what asks the questions. The language it asks in is SQL.
You already have MySQL if you installed XAMPP. Open http://localhost/phpmyadmin and you are looking at it.
Why not just save it to a file?
A fair question, and the honest answer is that for ten records you could. file_put_contents() works. The trouble starts the moment you need to find something.
Say you keep users in a file, one per line. To log somebody in you read the whole file and loop until the email matches. With fifty users that is instant. With fifty thousand it is fifty thousand comparisons on every single login.
Then it gets worse. Two people sign up in the same second, both read the file, both add their line, and one quietly overwrites the other. You want the ten highest scores, so you load everything into memory and sort it there. You want to change one field on one record, so you rewrite the entire file to do it.
A database is the program that has already solved all of that, and solved it properly. WHERE email = ? finds the row without reading the rest. Two writes at the same moment do not collide. Sorting and filtering happen before the data ever reaches PHP.
So what PHP MySQL actually buys you is not storage โ PHP can already write a file on its own. It is fast, safe answers to questions about your data, and that is the part you would otherwise have to build yourself, badly. Everything else in this PHP MySQL lesson is just learning how to ask.
Making a table to work with
In phpMyAdmin, create a database called flow_demo, then run this in the SQL tab.
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
marks INT NOT NULL
);
INSERT INTO students (name, marks) VALUES
('Ali', 82),
('Sara', 91),
('Bilal', 68);
Output

AUTO_INCREMENT PRIMARY KEY means MySQL hands out the id itself, and no two rows can share one. You will almost always want that on an id column.
Connecting PHP to MySQL
PHP has two ways to talk to MySQL. This lesson uses PDO, because the same code works with other databases and because it makes the safe way the easy way. The other extension, MySQLi, can be written as plain functions with no classes at all โ that version is PHP MySQLi: connect and query without PDO.
<?php
$host = "localhost";
$db = "flow_demo";
$user = "root";
$pass = "";
try {
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected!";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Output

On XAMPP the user is root and the password is empty. That is fine on your own machine and never fine on a real server.
The try and catch matter. ERRMODE_EXCEPTION tells PDO to throw a real error when something goes wrong instead of failing silently โ which is how people end up staring at a blank page for an hour.
Every example below carries on from this connection. If you put them in separate files, save the code above as db.php and start each one with require "db.php"; โ otherwise $pdo will not exist and PHP will say so.
Reading rows
<?php
$sql = "SELECT id, name, marks FROM students";
$rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo $row["name"] . " scored " . $row["marks"] . "<br>";
}
?>
Output

Look at what came back: an array of associative arrays. Exactly the shape from PHP arrays, and you walk it with the same foreach.
That is the whole trick. A database result is just an array, and you already know arrays.
Inserting data safely
Here is the part that matters most in this lesson, so read it twice.
Never paste user input straight into SQL. Use a prepared statement: you write the query with placeholders, then hand the values over separately.



Loading comments...