How to Run a PHP Script? Step By Step Guide!


Hello and welcome to the start of codeofaninja.com’s series of web development articles!

Contents of this post include:

1.0 Overview
2.0 Install XAMPP

3.0 Run Your First PHP Script
3.1 Go to XAMPP server directory
3.2 Create hello.php
3.3 Code Insie hello.php
3.4 Open New Tab
3.5 Load hello.php
3.6 Output

4.0 Manage Database with PhpMyAdmin
4.1 Create a Database
4.2 Create a Table
4.2 Insert Data
4.4 Useful Videos

5.0 Run PHP Script with Database
5.1 Go to XAMPP server directory
5.2 Create read_one.php
5.3 Code Inside read_one.php
5.4 Open Your Browser
5.5 Load read_one.php
5.6 Output

6.0 Online Resources
7.0 What’s Next?
8.0 Some Notes

1.0 Overview

Setting up a development environment for PHP programming is easy. Download the code editor you prefer, I personally like atom.io text 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 main question: Mike, how to run a PHP script? This post is my answer to you guys and to those people who will need this in the future.

In the following tutorial, we will learn how to install XAMPP, how to run a PHP script, manage database with PhpMyAdmin and run a sample PHP script that fetches a record from the database.

2.0 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.

3.0 Run Your First PHP Script

The following is an example about how to run a PHP script. What this program does is show a “Hello World!” text on the screen or webpage.

3.1 Go to XAMPP server directory

I’m using Windows, so my root server directory is “C:\xampp\htdocs\”.

3.2 Create hello.php

Create a file and name it “hello.php”

3.3 Code Insie hello.php

Open hello.php and put the following code.

<?php
echo "Hello World!";
?>

3.4 Open New Tab

Run it by opening a new tab in your browser

3.5 Load hello.php

On you browser window, type http://localhost/hello.php

3.6 Output

You should see the following output.
run-php-script-1
Great job, you just run a PHP script!

4.0 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 the use of a web browser. In the following examples, we will see how easy we can handle MySQL with PhpMyAdmin.

4.1 Create a Database

  1. Go to http://localhost/phpmyadmin/
  2. Click the “New” link on the upper left corner (under recent tables)
  3. Fill out the “Database Name” field with “my_first_database“.
  4. Click the “Create” button

1-create-first-database

4.2 Create a Table

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

2-create-table

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

3-create-fields-of-table

4.3 Insert Data

Click the “products” table.

4-click-table-to-insert-data

Click the “Insert” tab.

5-click-table-to-insert-data-2

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

6-fill-out-form-to-insert-data

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

7-data-was-inserted

4.4 Useful Videos

1. Create a database and import MySQL file.

2. Create a database and create table.

5.0 Run PHP Script with Database

In the following steps, we will run a PHP script that fetches one record from the MySQL database.

5.1 Go to XAMPP server directory

Go to your “C:\xampp\htdocs\” directory

5.2 Create read_one.php

Create a file and name it “read_one.php”

5.3 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>";
?>

5.4 Open Your Browser

Run it by opening you your browser

5.5 Load read_one.php

On you browser window, type http://localhost/read_one.php

5.6 Output

You should see the following output.
run-php-script-2
Awesome! You are now ready to learn more about web programming and development.

6.0 Online Resources

Here in codeofaninja.com, we want to simplify learning for you to actually build something. But it is also important for you to read and study more. The following are my suggestions where to learn more.

You can always go back to the list above while you go along our series of web programming tutorials.

7.0 What’s Next?

Today we have learned how to run a PHP script.

Before we learn more of PHP & MySQL programming, let us learn how to use a front-end framework that will beautify the user-interface of simple applications that we will build.

Up next: Learn Bootstrap Tutorial for Beginners

8.0 Some Notes

#1 Found An Issue?

If you found a problem with this code, we can solve it faster via Email or FB message, please send me a message via email mike@codeofaninja.com, or via our official Facebook page!

Please be more detailed about your issue. Best if you can provide an error message and your test or page URL. Thanks!

Please feel free to comment if you have any questions, suggestions, found something wrong or want to contribute to this code.

#2 Become a true Ninja!

We constantly add new tutorials and improve our existing tutorials and source codes. Be one of the first to know an update by subscribing to our FREE newsletter. Get a FREE EBOOK as well. CLICK HERE TO SUBSCRIBE FOR FREE!

#3 Thank You!

Please share this post if you think this is a useful tutorial about how to run a PHP script. Thanks for learning here in codeofaninja.com!