Google Ads

Retrieving Data in MySQL/PHP v2 PHP Script


Retrieving Data in MySQL/PHP v2 PHP Script

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

Required:
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.

Done!

Important:
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…

 

 
 
  1. <?php
  2.       $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
  3.       $q = mysqli_query($con, "SELECT * FROM `test`"); //Gets all from table 'test'
  4.       while ($row = mysqli_fetch_array($q)) {
  5.             //$row = next row of results/all rows from table 'test' within database 'fln'
  6.             echo $row['id']; //Echo current $row's column value of 'id'.
  7.       }
  8. ?>

 

 

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).

 

 

 


Related Post

Php Sheep and Goat Management Application PHP Scri... Php Sheep and Goat Management Application PHP Script Goat and Sheep Management is a web application design to management any animal husbandry farm ...
Library Management System v3 PHP Script Library Management System v3 PHP Script Hello guys i have here a Library Management System for E.B magalona national high school this is a system t...
VIN Decoder Vehicle PRO + Epicvin Affiliate PHP Sc... VIN Decoder Vehicle PRO + Epicvin Affiliate PHP Script VIN Decoder Vehicle is a simple system to obtain information of any vehicle from its VIN num...
Network Speed Tester v1.2 PHP Script Download PHP Network Speed Tester is PHP based script to offer a network bandwidth check against your internet server. No database or long configuration requir...
Advanced Menu Manager System PHP Script Download Advanced Menu Manager SystemAdvanced Menu Manager SystemAs the name says, it’s a PHP and JQuery robust menu manager application. Main feature about ...

Have any Question or Comment?

Leave a Reply

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

"