Retrieving Data in MySQL/PHP v2 PHP Script

Retrieving Data in MySQL/PHP v2 PHP Script

This tutoral will show you how to simply retrieve data using PHP from a MySQL database.

Before you can retrieve the data, you must have the following:
1) A webhost or localhost.
2) A database in your MySQL service.
3) A table with data in your MySQL database.

Creating a Database:
To create a database in your MySQL service, simply go to the MySQL Databases feature of your cPanel (Control Panel) and create a new database with your preferred name. If it was a database to hold the information about a sales site named ‘biosales’, you might call it ‘biosalesdb’ for simplicity.

Creating a Table:
To create a table within your database, go to the PHPMyAdmin feature of your cPanel, select your database in the left hand side column, enter a name in the ‘Create a Table’ seciton of the page, enter the amount of columns the table should have, and click ‘Create’.

You will now be redirected to the table creation page, here you can set the columns your table will have. For this tutorial, we simply need an ID column. Every table you create should have an ID column.

To create the ID column, type ‘id’ in to the name of the column box, set the value type to ‘Integer/Int’, set the length to 5 (values up to 99999), set the index to ‘Primary Key’ and set ‘AI/A_I/Auto Increment’ to true/checked/yes. Click save/create.


Don’t forget to put data in to your table. To do this, simply select your table, click on ‘Insert’, type in some rows of data and then click ‘Save’.

PHP – Connection:
Now, on to the PHP script. The following code is commented so I will only document it minimally…



          $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
          $q = mysqli_query($con, "SELECT * FROM `test`"); //Gets all from table 'test'
          while ($row = mysqli_fetch_array($q)) {
                //$row = next row of results/all rows from table 'test' within database 'fln'
                echo $row['id']; //Echo current $row's column value of 'id'.



So the above code;
First creates the connection to the databaes through the MySQL service taking four parameters of the server, username, password and database name (ensure you have the correct database name. On most web hosts, you will need to add your username/user id followed by an underscore, ‘_’, before the ‘database name’). It stores the connection in a ‘con’ variable.

Next, it stores a query in a ‘q’ (standing for ‘query’) variable. This query takes the connection of ‘con’, and runs the query;
SELECT * FROM `test` – this selects all (*) the rows of information within the table ‘test’… which we created earlier.

After getting the rows we then perform a while loop. This while loop iterates through each row of data returned by our earlier query and stores each row iteration in a new ‘row’ variable which is temporary for the while loop (meaning you can’t use it outside of the while loop).

Finally, we simply echo/print/output to the screen the value held within the current row (variable; ‘row’) and column of ‘id’; which should increase by one per row (since we selected the Auto Increment option while creating our table earlier in this tutorial).




Learn How to install Retrieving Data in MySQL/PHP v2 PHP Script?

Find More related Retrieving Data in MySQL/PHP v2 PHP Script

Have any Question or Comment?

Leave a Reply

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