Welcome to the world of PHP and MySQL! Imagine you’re about to embark on a journey to build dynamic, interactive websites. PHP is like your trusty toolkit, and MySQL is your organized library where all your data is stored. Together, they allow you to create powerful web applications. Let’s start by understanding the basics of each.
What is PHP?
PHP stands for Hypertext Preprocessor. It is a server-side scripting language designed specifically for web development. PHP code is executed on the server, and the result is sent to the client’s web browser as plain HTML. Here are some key features of PHP:
What is MySQL?
MySQL is an open-source relational database management system (RDBMS). It uses SQL (Structured Query Language) to manage and manipulate databases. Key features of MySQL include:
To start working with PHP and MySQL, you’ll need to set up a local development environment. Tools like XAMPP, WAMP, or MAMP provide a complete package including Apache (web server), PHP, and MySQL.
Step 1: Installing XAMPP
Step 2: Creating a Database in MySQL
Now that your environment is set up, let’s write a simple PHP script.
1. Create a PHP File: Open a text editor and create a new file named index.php.
2. Write PHP Code:
php
Copy code
<?php
echo “Hello, World!”;
?>
3. Save the File: Save the file in the htdocs directory of your XAMPP installation (e.g., C:\xampp\htdocs\index.php).
4. Run the Script: Open your web browser and go to http://localhost/index.php. You should see “Hello, World!” displayed on the page.
Let’s create a simple PHP script to connect to the MySQL database.
1. Create a Database Table:
Click on the “SQL” tab and run the following SQL query to create a table:
SQL
Copy code
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
2. Insert Sample Data:
SQL
Copy code
INSERT INTO users (username, password) VALUES (‘admin’, ‘admin123’), (‘user’, ‘user123’);
3. Create a PHP Script to Connect to the Database:
Write the following PHP code:
php
Copy code
<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “mydatabase”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
echo “Connected successfully”;
?>
Save the file in the htdocs directory.
4. Run the Script: Open your web browser and go to http://localhost/connect.php. You should see “Connected successfully”.
Let’s extend our script to retrieve data from the users table.
1. Update the connect.php Script:
php
Copy code
<?php
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “mydatabase”;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
echo “Connected successfully<br>”;
// SQL query to retrieve data
$sql = “SELECT id, username 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”]. ” – Username: ” . $row[“username”]. “<br>”;
}
} else {
echo “0 results”;
}
$conn->close();
?>
2. Run the Script: Open your web browser and go to http://localhost/connect.php. You should see the list of users.
In this introductory chapter, you’ve learned the basics of PHP and MySQL. You’ve set up a local development environment, written your first PHP script, created a MySQL database, connected PHP to MySQL, and retrieved data from the database. This foundational knowledge will serve as the building blocks for more advanced PHP and MySQL development. Keep practicing, and you’ll soon be creating dynamic, interactive web applications. Happy coding!