The ancient web development setup: PHP and MySQL with XAMPP
Are you ready to dive into the world of web development? Great! Setting up your development environment is one of the first hurdles you’ll face. Don’t worry — we’ve got you covered. In this guide, we’ll walk you through setting up PHP and MySQL using XAMPP, a popular tool that makes the process a breeze.
Introduction
Before we jump into the setup, let’s briefly cover what PHP and MySQL are and why they’re important:
- PHP is a server-side scripting language used for creating dynamic web pages.
- MySQL is a popular database system that stores and manages data for web applications.
Together, they form the backbone of many websites and web applications. But to start developing with PHP and MySQL, you need a local environment on your computer. That’s where XAMPP comes in.
Understanding XAMPP
XAMPP is a free, open-source package that bundles together several tools you need for web development:
- X — Cross-platform (works on Windows, macOS, and Linux)
- A — Apache (the web server)
- M — MySQL (the database)
- P — PHP (the programming language)
- P — Perl (another programming language, but we won’t focus on this)
XAMPP creates a local server environment on your computer, allowing you to test and develop websites without needing an internet connection or a remote server.
XAMPP Setup
XAMPP from the official website: https://www.apachefriends.org/
Download XAMPP from the official website and follow their instruction to install it on your computer.
After successful installation, start the services and verify:
- Open XAMPP
- Start PHP and MySQL services
- Visit a Web browser (My preference: Google Chrome):
localhost
Setting Up Your First PHP Project
Let’s create a simple PHP file to make sure everything’s working:
- Locate the
htdocs
folder:
- Windows:
C:\xampp\htdocs
- macOS:
/Applications/XAMPP/xamppfiles/htdocs
- Linux:
/opt/lampp/htdocs
2. Create a new file called hello.php
in the htdocs
folder.
3. Open hello.php
in a text editor and add the following code:
<?php echo "Hello, World! This is PHP running on XAMPP."; ?>
4. Save the file and open a web browser.
5. Navigate to http://localhost/hello.php
If you see the message “Hello, World! This is PHP running on XAMPP.”, congratulations! 🎉 You’ve successfully set up PHP with XAMPP.
Introduction to MySQL and phpMyAdmin
Now let’s set up a database:
In your web browser, go to http://localhost/phpmyadmin/
Log in with the default credentials:
- Username: root
- Password: (leave blank)
Create a new database:
- Click “New” in the left sidebar.
- Enter a name for your database (e.g., “my_first_db”).
- Click “Create”.
Create a new user:
- Go to the “User accounts” tab.
- Click “Add user account”.
- Fill in a username and password.
- Under “Global privileges”, check “Check all”.
- Click “Go” at the bottom to create the user.
Security Tip: In a real-world scenario, you’d want to be more selective with user privileges, but for learning purposes, we’re keeping it simple.
Connecting PHP to MySQL
Let’s create a PHP script that connects to our new database:
- Create a new file called
db_test.php
in yourhtdocs
folder. - Add the following code:
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "my_first_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
// don't forget to replace "your_username", "your_password",
// and "my_first_db" with the actual values
If you have provided a valid username
, password
, and dbname
then visit the URL: localhost/db_test.php
and you’ll see Connected successfully
message.
Best Practices and Security Considerations
While this setup is great for learning, remember these points for the future:
- Keep XAMPP updated to get the latest security patches.
- For real projects, always use strong, unique passwords.
- Be cautious about what you share online — don’t post database credentials or sensitive information.
Thank you for taking the time to read this. As you embark on your journey into web development using PHP and MySQL, I wish you all the best. This adventure will enhance your technical skills and open up new opportunities for creativity and problem-solving. Embrace the challenges, stay curious, and enjoy the process of building dynamic and interactive web applications. Good luck!