Welcome to PHP MySQL tutorial! In this comprehensive guide, we'll learn how to work with databases using PHP and MySQL, a powerful combination for web development.
By the end of this lesson, you'll be able to create, read, update, and delete data from a MySQL database using PHP. Let's dive in!
MySQL is an open-source relational database management system (RDBMS) that is widely used for web applications. It's known for its speed, reliability, and ease of use.
PHP (Hypertext Preprocessor) is a popular open-source scripting language for web development. It's used to create dynamic and interactive websites and applications.
PHP and MySQL are a great combination for web development because they complement each other. PHP is used to interact with the MySQL database, while MySQL is used to store and manage data.
To get started, make sure you have the following installed on your computer:
You can download both from the Official PHP Website and Official MySQL Website.
To connect to a MySQL database using PHP, we'll use the mysqli extension. Here's a simple example of how to create a connection:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>π Note: Replace your_username, your_password, and your_database with your actual MySQL credentials and database name.
Let's create a simple table called users:
CREATE DATABASE my_database;
USE my_database;
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);Now, let's insert some data into our users table using PHP:
// Insert data
$sql = "INSERT INTO users (name, email, password) VALUES ('John Doe', 'john@example.com', 'password123')";
if ($conn->query($sql) === TRUE) {
echo "New user created successfully!";
} else {
echo "Error: " . $conn->error;
}To retrieve data from the users table, we'll use PHP to execute a SELECT query:
// Retrieve data
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "No users found.";
}To update data in the users table, we'll use PHP to execute an UPDATE query:
// Update data
$sql = "UPDATE users SET email='john_updated@example.com' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "User updated successfully!";
} else {
echo "Error: " . $conn->error;
}To delete data from the users table, we'll use PHP to execute a DELETE query:
// Delete data
$sql = "DELETE FROM users WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "User deleted successfully!";
} else {
echo "Error: " . $conn->error;
}What does the `mysqli` extension in PHP do?
That's it for this lesson! In the next lessons, we'll dive deeper into working with PHP and MySQL, including more advanced topics like prepared statements, transactions, and more. Happy coding! π₯³π»π