KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Retrieve multiple records from MySQL database using php

Before you start this lesson, I assume that you finished reading the following tutorials:

  1. install phpmyAdmin, mysql , php
  2. Create Database by using phpMyAdmin
  3. Working with forms
  4. Insert Form Data into Database
  5. Retrieve data from Database

In this lesson, we will learn how to retrieve multiple records from MySQL database and display them in a table.

PHP Retrieve data from Mysql

from the lesson Retrieve data from Database, we already know how to use mysql functions in PHP the following 3 things:

  1. connect to database mysql_connect(localhost,$username,$password);
  2. Query the database mysql_query(query string)
  3. Get resultmysql_result(result resource,int row,[column name]).

Now we need learn how to display multiple records.

Counting how many rows in MySQL query result

Before we do the loop through the data in retrieved result variable, we need know how many records(rows) the results have. We use the command the count how many records in the results.


Using while loop

Once we got the records count, we could set up a loop to go through the results like following:

//get result.
$row++;//increase by 1;

Note that: PHP count records from 0. 0 is the first record, 1 is the second and so on.

Put result into table

We may use the above loop to put records in to HTML table

echo "<table border=1 >";
//get name and phone number from results
echo "<tr><td>$name</td><td>$phone</td></tr>";
$row++;//increase by 1;
echo "</table>";

Put together

We put all code together as following.

$password="123456"; //input your password here.
//connect to database
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
//query databae
$query = "select * from contact";
$result=mysql_query($query) or die('Error, insert query failed');
echo "<table border=1>";
echo "<tr><td>$name</td><td>$phone</td></tr>";
echo "</table>";

Save above code to .php file in your web root or any other virtual directory. Launch this php file in your browser like http://localhost/mycode.php.

You should be able to see a table of your records.

PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Logic Structures
Include files
Set cookie and delete cookie
Most often used MySQL functions



Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © All rights reserved(new). Privacy|Copyright
designed by

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer