Hello, and welcome to the start of codeofaninja.com’s series of web development tutorials!
Overview
Setting up a development environment for PHP programming is easy. Download your preferred code editor, and I like the Visual Studio code editor.
Next is to install XAMPP, the most popular PHP development environment. This package contains Apache, PHP & MariaDB, or MySQL database applications.
Many people emailed me with a primary question: Mike, how to run a PHP script? This post is my answer to you and those needing this in the future.
In the following tutorial, we will learn how to install XAMPP, run a PHP script, manage the database with PhpMyAdmin and run a sample PHP script that fetches a record from the database.
Install XAMPP
Go to this link and download XAMPP for your operating system. XAMPP is available for Windows, Linux, or Mac.
Here’s a video about how you can install and use XAMPP.
Run Your First PHP Script
The following is an example of how to run a PHP script. This program shows a “Hello World!” text on the screen or webpage.
Go to the XAMPP server directory.
I’m using Windows, so my root server directory is “C:\xampp\htdocs\”.
Create hello.php
Create a file and name it “hello.php
“
Code Inside hello.php
Open hello.php
and put the following code.
<?php echo "Hello World!"; ?>
Open New Tab
Run it by opening a new tab in your browser
Load hello.php
On your browser window, type http://localhost/hello.php
Output
You should see the following output.
Great job, you just ran a PHP script!
Manage MySQL with PhpMyAdmin
MySQL is an open-source relational database management system (RDBMS). MySQL is a popular choice of database for use in web applications.
phpMyAdmin is a free and open-source tool written in PHP intended to handle the administration of MySQL with a web browser. In the following examples, we will see how easily we can handle MySQL with PhpMyAdmin.
Create a Database
- Go to http://localhost/phpmyadmin/
- Click the “New” link in the upper left corner (under recent tables)
- Fill out the “Database Name” field with “my_first_database“.
- Click the “Create” button

Create a Table
- Click “
my_first_database
” on the left side of the screen - On the “Create Table” section, fill out the Name with “products” and the Number of Columns with “6“
- Click the “Go” button

- Fill out the fields with id, name, etc.
- Mimic everything in the following image
- Click the “Save” button

Insert Data
Click the “products” table.

Click the “Insert” tab.

Fill out the form, mimic the data on the following image. Click the “Go” button.

Great job! We now have a database, a table inside the database, and a record inside the table.

Useful Videos
1. Create a database and import MySQL files.
2. Create a database and create a table.
Run PHP Script with Database
In the following steps, we will run a PHP script that fetches one record from the MySQL database.
Go to the XAMPP server directory
Go to your “C:\xampp\htdocs\” directory
Create read_one.php
Create a file and name it “read_one.php
“
Code Inside read_one.php
The numbers 1-8 in the following code are called “code comments”. It explains each part of our simple code below. Open read_one.php
and put the following code.
<?php // 1. database credentials $host = "localhost"; $db_name = "my_first_database"; $username = "root"; $password = ""; // 2. connect to database $con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password); // 3. prepare select query $query = "SELECT id, name, description, price FROM products WHERE id = ? LIMIT 0,1"; $stmt = $con->prepare( $query ); // 4. sample product ID $product_id=1; // 5. this is the first question mark in the query $stmt->bindParam(1, $product_id); // 6. execute our query $stmt->execute(); // 7. store retrieved row to the 'row' variable $row = $stmt->fetch(PDO::FETCH_ASSOC); // 8. show data to user echo "<div>Name: " . $row['name'] . "</div>"; echo "<div>Description: " . $row['description'] . "</div>"; echo "<div>Price: $" . $row['price'] . "</div>"; ?>
Open Your Browser
Run it by opening your browser
Load read_one.php
On your browser window, type http://localhost/read_one.php
Output
You should see the following output.
Awesome! You are now ready to learn more about web programming and development.
What’s Next?
When building web applications, a presentable user interface is a must. We do not want our users to have a hard time using what we built.
So, for our next tutorial, we will learn a CSS framework called Bootstrap. It is easy to learn, and we will use it for the PHP web application we build in this series.
Click the following link for the next tutorial: Bootstrap Tutorial for Beginners
Online Resources
Here at codeofaninja.com, we want to simplify learning and for you to build something unique on the web. But it is also essential for you to read and study more. The following are my suggestions for where to learn more.
- PHP Manual – official PHP documentation.
- PHP The Right Way – easy-to-read, quick reference for PHP popular coding standards and more.
- PHP Delusions – proper use of PHP programming language, proper use, with the focus on disproving various delusions and superstitions.
- PHP Cheat Sheets – Variable comparison, arithmetic and testing cheat sheet.
- PHP FAQs – Frequently asked PHP questions
- PHP Security Cheat Sheets – prevent attacks like XSS, SQL Injection and more.
- Cross-Site Scripting Attacks (XSS) – Another good resource to learn and prevent cross-site scripting (XSS).
You can always go back to the list above with our web programming tutorials.
[adinserter block=”3″]
Leave a Reply