How to connect the website to MySQL DataBases

In the last post, I wrote about using PHP to communicate with MySQL Databases. In this post, I show you a SQL command used to get some data from DataBases.

Let’s start with MySQL Databases connection

In my post How to use PHP to communicate with SQL Base, I showed you a simple, but quite a safe way to connect to MySQL Databases. You can check this out now before we go further. What’s more, you need to have at least one database setup on SQL Server, before you connect to it from the website. So let’s build one if you don’t have any.

The easiest way to do that on the local computer is to install a LAMP stack such as XAMMP from apache friends. If you read the last post How to use PHP to communicate with the SQL base you probably have installed LAMP stack. If not, you should install it to build SQL Base before we connect to it.

Anyway, when you run your LAMP stack you will be able to connect with SQL Base. You can do it via console, with is cool of course, but you can also use PHPMyAdmin soft to manage your bases. I prefer to use PHPMyAdmin.

Databases website admin panel.


When you log in to the PHPMyAdmin panel you will see databases stored in SQL Base. we need to create a new one. In the next step you need to create a name for this database, let’s say the database we call Test_1 and select your language coding standard. I’m from Poland, so I chose utf8_polish_ci. Now you have a database that you can use to test some PHP commends to add and remove elements from the database.

Creating new table in databases.



In the last post, with I linked above, you can read how to build communication to the SQL Database via PHP. Let’s make this connection now, and add a new table to our database Test_1.

Code allows connecting with databases.


Executing code written above, you connect to your SQL Database named Test_1 and create new table “users”. You can check it in PHPMyAdmin dashboard.

Checked via website admin panel if the table is created.


When we have a table, we can save some data in this table. Let’s insert a few new users.

Code that put data to databases.


Finally, you have a database with data that can be used to build a dynamic website. How?

Building a dynamic website with PHP and SQL Databases

Building a dynamic website is like building new buildings from Lego bricks. You need some HTML structure, this is your Lego bricks, and then you get some data from SQL Databases and put it in these blocks. Is it hard to do? No. Look at this code below.

Code that builds a website from data grabbed from databases.

Just, like before, in the beginning, you need to connect to your Database. Then you select a table, from where you want to display data. You need to know how much data you receive, so you count numbers of rows. In the end, you build an HTML structure and put data into this structure.

This is an extra simple example but in fact, a more complicated example work in this same way. You have some HTML structure, grab data from Databases, and insert data to HTML. CMS like WordPress, Joomla, and so on can do this for you, but still worth knowing how its works.

No comments
Krzysztof NyrekHow to connect the website to MySQL DataBases

Related Posts

Leave a Reply

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

16 + 10 =