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


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

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

// Select all coulumns from one row.

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


In this tutorial, we'll create only 1 file.
1. select.php

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


replace with this

STEP1: Create table "test_mysql"

In this step, you have to create a table and insert data for testing our code.

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 '',

-- Dumping data for table `test_mysql`

INSERT INTO `test_mysql` VALUES (1, 'Billly', 'Blueton', '');
INSERT INTO `test_mysql` VALUES (2, 'Jame', 'Campbell', '');
INSERT INTO `test_mysql` VALUES (3, 'Mark', 'Jackson', '');
INSERT INTO `test_mysql` VALUES (4, 'Linda', 'Travor', '');
INSERT INTO `test_mysql` VALUES (5, 'Joey', 'Ford', '');
INSERT INTO `test_mysql` VALUES (6, 'Sidney', 'Gibson', '');

STEP2: Create file - Select.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";


<table width="400" border="1" cellspacing="0" cellpadding="3">



// Start looping rows in mysql database.

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

// close while loop



// close MySQL connection


STEP3: Run the Code

Similar Post
Connecting to MySQL database

Connect to MySQL database

This tutorial will show you how to connect to mysql database. It's easy to write a script to connect to world's most popular opensource database.
Order MySQL results

Order MySQL results

In this tutorial, you'll learn how to sort MySQL result. You can sort MySQL result by ascending, descending and random MySQL result.