All PHP Tutorials
Ad Management
Counter
Email System
Forum and Guestbook
File Upload
Image Manipulation
Login / Members / Password
Pagination
Encode Script
Refresh / Redirection
Miscellaneous
All MySQL Tutorials
Create, Manage Database using phpMyAdmin
Connect to Database
Insert Data
Select Data
Edit Database
Update Database
Delete Database
Order Results
Home > MySQL Tutorials
Select data From mysql
Selecting data From mysql
When you need data from your mysql database to show in your web page, you need to select database, table and what's row you want to pull its data first. 
 
Syntax

// Select all columns from all rows.
"SELECT * FROM table_name";

or
// Select some column from all rows.
"SELECT column_name1, column_name2 FROM table_name";

or
// Select all coulumns from one row.

"SELECT * FROM table_name WHERE column_name=' value in column '";


Overview

In this tutorial create 1 file
1. select.php

Step
1. Create table "test_mysql" in database "test".
2. Create file select.php.
3. test it!

If you don't want looping rows in mysql, replace
while($rows=mysql_fetch_array($result)){
........
<?php
}
mysql_close();
?>


replace with this
$rows=mysql_fetch_array($result);
.........
<?php
mysql_close();
?>



Create table "test_mysql"


CREATE TABLE `test_mysql` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=7 ;

--
-- Dumping data for table `test_mysql`
--

INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', 'bb5@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', 'jame@somewhere.com');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', 'mark@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', 'lin65@phpeasystep.com');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', 'fordloi@somewhere.com');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', 'gibson@phpeasystep.com');




Create file - Select.php
Diagram


View In Browser



############### Code

<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>

<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="10%"><? echo $rows['id']; ?></td>
<td width="30%"><? echo $rows['name']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['email']; ?></td>
</tr>
</table>

<?
// close while loop
}

// close connection
mysql_close();
?>

 

Random Tutorial
 
PHP Script Image of the day
In this script shows you how to display image of the day. You can adapt this script to display quote of the day, knowledge of the day or something else you want. You don't have to change image everyday just change images once a week. 
 
 
   
Hosted by Hostgator
© PHPeasystep.com 2005-2007