Recently I tutored someone that needed help with “HTML”. I put the quotes around HTML because once I learned more about what they were trying to accomplish, I learned that it involved more than HTML.
They required PHP. It’s been a few years since I touched anything PHP. So I had to get set up. I was pleasantly surprised to learn how easy it is to get up and running with PHP.
Like anything else, getting up and running is relatively easy if you know what you are doing. So I decided to write this post to have the set up running all in one place.
This post will cover the following:
- Downloading and installing XAMPP – https://www.apachefriends.org/index.html
- Setting up MariaDB (non propietary version of MySQL since Oracle owns MySQL).
- Setting up a user and log in
- Creating a mini application in PHP to save data to the Database.
XAMPP stands for Apache + MariaDB + PHP + Perl. To download XAMPP just visit their website, download it and install it.
Once you download it. After you install it run the XAMPP control panel to start PHP and MySQL.
To configure MySQL click on the Admin button for MySQL from the XAMPP panel. That will take you to the localhost/phpmyadmin URL. From there you can do all things MySQL such as create a database and add users.
So.. let’s create a database… For my purposes I am creating a database and naming it bpdemo (important note that I goofed and used pbdemo for some things and bpdemo for others. Everything should be bpdemo whereever you see pbdemo include username, password, database and applicaiton folder). Leave the defaults and create it.
You can use the GUI or use SQL to create tables. It’s best to use SQL to create tables that way you can share the scripts and stand up your database in any other environment. So let’s create a table using the following script.
DROP TABLE IF EXISTS bpdemo.books; CREATE TABLE bpdemo.books ( book_id int PRIMARY KEY, title varchar(50), author varchar(255), pages int ); INSERT INTO bpdemo.books (`book_id`, `title`, `author`, `pages`) VALUES (1,'html 5', 'J. Doe', 431), (2,'awesome css', 'J. Doe', 115), (3,'Beginning C#', 'J. Williams', 231), (4,'JS for Beginners', 'A. Beginner', 541)
To create the table simply select the database that you created and then select the SQL tab as shown below.
There is a “go” button down at the bottom left that you must click in order for the SQL to statements to run. So.. drop the code and click the “go” button.
You should now have table populated like show below in your pbdemo database.
Now that you have the database created with a table. let’s go ahead and create a user to access it. On the the right side of the panel select the priviledges tab and create a user.
In our case lets create the user pbdemo with the password pbdemo.
You’ll want to give that user access to all CRUD (Create, Read, Update, Delete)
Creating Your PHP Page
To create a page that is accessible you just have to add your PHP page in the following folder.
That is assuming you installed XAMPP in that location. Otherwise you will have to find htdocs in your XAMPP installation folder.
So let’s create page to read in our books table. In that pbdemo folder drop the file named index.php with the following content.
View the gist below to see the code for the for index.php
The page above is simple an HTML page with php code in it and named index.php. The first thing that we must do is connect to the database.
Once we connect we run a query for the books table data.
Once we fetch the data we iterate through the records to generate our table content to display the books.
The final display is shown below.
The intent of this post was to show you how to get up and running with XAMPP. There are many things that we did not cover. For instance writing all your code inline is bad practice. Instead you should separate your PHP code into the standard separate layers of concerns (Presentation, Data Access and Business Logic).
There are PHP frameworks to facilitate this. You can find more about these by googling things like MVC PHP frameworks.
Also your credentials should be stored in your standard configuration, however that may be, either through config files or in a database.
Also you may be new to PHP, so you may have to take a course to read the basics. In this example I use minimal PHP. The words that start with dollar sign such as $conn are just variables in PHP. You may have also noticed that I concatenat strings like this $row[‘book_id’] . “</td>”. The dot operator is a way in PHP to join strings.
This example is enough to get you started though. If you have any questions or comments feel free to drop them down below.