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

Retrieving PHP Script | Data PHP Script | In PHP Script | Mysql PHP Script | Php PHP Script | V2 PHP Script | Php PHP Script | Script PHP Script | Introduction PHP Script | Th PHP Script | Tutoral PHP Script | Will PHP Script | show PHP Script | How PHP Script | To PHP Script | Simply PHP Script | Retrieve PHP Script | Data PHP Script | Using PHP Script | Php PHP Script | From PHP Script | A PHP Script | Mysql PHP Script | Database PHP Script | Required PHP Script | Bee PHP Script | Can PHP Script | Retrieve PHP Script | 2021 Top 100 Retrieving PHP Script | Top 10 Data PHP Script | Top 50 In PHP Script | 2021 Top 40 Mysql PHP Script | Top 100 Php PHP Script | Top 50 V2 PHP Script | 2021 Top 100 Php PHP Script | Top 20 Script PHP Script | Top 100 Introduction PHP Script | Top 10 Th PHP Script | Top 100 Tutoral PHP Script | Top 200 Will PHP Script | Top 500 show PHP Script | Top 5000 How PHP Script | Top 50 To PHP Script | Top 50 Simply PHP Script | Top 50 Retrieve PHP Script | Top 50 Data PHP Script | Top 50 Using PHP Script | 2021 Top 50 Php PHP Script | Top 50 From PHP Script | Top 50 A PHP Script | Top 50 Mysql PHP Script | Top 50 Database PHP Script | Top 20 Required PHP Script | Top 20 Bee PHP Script | Top 10 Can PHP Script | Top 10 Retrieve PHP Script | Popular 100 Retrieving PHP Script | Popular 10 Data PHP Script | Popular 50 In PHP Script | Popular 40 Mysql PHP Script | Popular 100 Php PHP Script | Popular 50 V2 PHP Script | Popular 100 Php PHP Script | Popular 20 Script PHP Script | Best 100 Introduction PHP Script | Best 10 Th PHP Script | Best 100 Tutoral PHP Script | Best 200 Will PHP Script | Best 500 show PHP Script | Best 5000 How PHP Script | Popular 50 To PHP Script | 2021 New 50 Simply PHP Script | 2021 New 500 Retrieve PHP Script | 2021 New 1000 Data PHP Script | 2021 Top 500 Using PHP Script | 2021 Best 500 Php PHP Script | 2021 New 10 From PHP Script | 2021 Popular 500 A PHP Script | 2021 Popular 20 Mysql PHP Script | 2021 New 1050 Database PHP Script | 2021 Top 200 Required PHP Script | 2021 Top 4000 Bee PHP Script | 2021 Best 100 Can PHP Script | 2021 Best 120 Retrieve PHP Script |

Have any Question or Comment?

Leave a Reply

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