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

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.
run-php-script-1
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

  1. Go to http://localhost/phpmyadmin/
  2. Click the “New” link in 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

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 the Number of Columns with “6
  3. Click the “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

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

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.
run-php-script-2
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.

You can always go back to the list above with our web programming tutorials.

[adinserter block=”3″]


Comments

10 responses to “How to Run a PHP Script? Step By Step Guide!”

  1. Jonhard Larsen Avatar
    Jonhard Larsen

    Hi Mike, what PHP version do you recommend?

    1. Hi @jonhardlarsen , PHP 5.4 works for most of my projects. But to get the most out of PHP, I recommend the latest version PHP 7.

  2. Umesh Kashyap Avatar
    Umesh Kashyap

    Hi Mike,

    I just new to php things, please help. Somehow my “read_one.php” not works well. I follow all the steps you have mentioned. Unable to find the problem.

    Code:
    prepare( $query );

    $product_id=1;

    $stmt->bindParam(1, $product_id);

    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    echo “Name: ” . $row[‘name’] . “”;
    echo “Description: ” . $row[‘description’] . “”;
    echo “Price: $” . $row[‘price’] . “”;
    ?>

    1. Hi @disqus_2SNvf2MCTr, I’m unable to replicate the issue, would you send a screenshot of the error message you encountered?

      1. Umesh Kashyap Avatar
        Umesh Kashyap

        Thanks Mike for reply.

        That issue is resolved.

        1. You’re welcome @disqus_2SNvf2MCTr!

  3. You’re welcome @disqus_A7IIEeTgEm! Please subscribe for more: https://www.codeofaninja.com/subscribe

  4. How when you run http://localhost/hello.php you get http://localhost/sample.php running and giving result?
    3.6 OUTPUT???

    I could not see any config section where the localhost is connected to the .php file and so getting HTTP Error 404.0 – Not Found error.

  5. Kliens Avatar
    Kliens

    I’m getting #1075 error message as I’m trying to save products table in PHPmyadmin database

    1. Hi Kliens, would you show us your query? I link to a screenshot would help us debug this issue.

Leave a Reply

Your email address will not be published. Required fields are marked *