Hello, and welcome to the start of codeofaninja.com’s series of web development tutorials!
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.
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.
The following is an example of how to run a PHP script. This program shows a “Hello World!” text on the screen or webpage.
I’m using Windows, so my root server directory is “C:\xampp\htdocs\”.
Create a file and name it “hello.php
“
Open hello.php
and put the following code.
<?php echo "Hello World!"; ?>
Run it by opening a new tab in your browser
On your browser window, type http://localhost/hello.php
You should see the following output.
Great job, you just ran a PHP script!
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.
my_first_database
” on the left side of the screenClick 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.
1. Create a database and import MySQL files.
2. Create a database and create a table.
In the following steps, we will run a PHP script that fetches one record from the MySQL database.
Go to your “C:\xampp\htdocs\” directory
Create a file and name it “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>"; ?>
Run it by opening your browser
On your browser window, type http://localhost/read_one.php
You should see the following output.
Awesome! You are now ready to learn more about web programming and development.
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
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″]