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

Retrieve data 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

In this lesson, we will learn how to retrieve data from MySQL database.We will retrieve phone number based on name input.

Collect input Form

We build a "form" like the following

<form action="action.php" method="post">
<input type="text" name="name"><br>
<input type="submit">
</form>

Save the above code to a HTML file in your c:\inetpub\wwwroot(see Install phpMyAdmin,MySql, PHP for detail)

PHP Retrieve data from Mysql

In the lesson Working with Forms , we already know how to get form input in php.

We use the following code to retrieve phone number associated with name input.

<html>
<body>
<?php
$username="root";
$password="123456";
$database="phonebook";
//connect to database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
$name=$_POST['name'];
//perform query
$query = "select * from contact where name='$name'";
$result=mysql_query($query) or die('Error, insert query failed');
//get result
$phone=mysql_result($result,0,"phonenumber");
echo "Hi! $name. Your phone number is <b> $phone</b> ";
?>
</body>
</html>

Save above code to "acion.php".

In the above code, we used two new Mysql function: mysql_query and mysql_result(). mysql_resul syntax is mysql_result(result resource,int row,[column name]). In this example, result resource is $result, row is 0(the first row), column name is "phonenumber".

Test it like the following:

Load the HTML file to your browser like http://localhost/myhtml.html, then type name in. You should be able to retrieve its phone number.

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
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
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 © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

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