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


WeBid v1.1.1 Auction PHP Script Download WeBid v1.1.1 Auction PHP Script Download WeBid is an open-source auction script written in the popular language PHP which a massive collection of i...
Kirby CMS Portal PHP Script Free Download Kirby CMS Portal PHP Script Free Download Kirby is a file-based CMS with a beautiful web interface and thousands of customers world-wide. Its flexi...
BeMusic v2.0.3 – Music Streaming Engine PHP ... BeMusic v2.0.3 - Music Streaming EngineBeMusic v2.0.3 - Music Streaming Engine BeMusic allows you to create your own music streaming website in min...
Geeklog CMS Portal PHP Script Free Download Geeklog CMS Portal PHP Script Free Download Geeklog is a PHP/MySQL based application for managing dynamic web content. "Out of the box", it is a bl...
Support Page PHP Script Download Support Page PHP Script Download This is a free support form script. This free utility software is developed using php and javascript. With this ut...

Read Me

Have any Question or Comment?

Leave a Reply

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

"