PHP simple : PHP tutorial, MySQL tutorial
PHP Tutorial - Free PHP Script

Home | Tutorials | Php vs Node.Js | Web Tools

MySQL : Select Record


This tutorial will show you how to get records in your table in database to show on your PHP files. This SQL statement has many options.

Syntax : Simple select statement. Select all columns from table.

select * from table_name;

Syntax : Select some columns from table.

select column_name1, column_name2, ... from table_name;

Syntax : Select from table with "where" statement. There are some different when you need the result. For Equal between record and value use "=". For Not Equal between record and value use "<>" or "!=". For just look like between record and value use "like", and "%" sign is using for take the place of some objects before or after specified value.

select * from table_name where column_name='value';
select * from table_name where column_name<>'value';
select * from table_name where column_name like '%value%';

Syntax : Select from table with "order by" statement. By default the result is sorted in ascending order or you can put asc directly into this command. To get a descending order you just change the asc with desc.

select * from table_name order by column_name;
select * from table_name order by column_name asc;
select * from table_name order by column_name desc;

Syntax : Select from table with "limit" statement. This statement need two number after like this "limit 0,5". The first number is for specify starting record (starting at 0) and plus to the next records by 5 times. That means you have get 5 records.

select * from table_name limit number1, number2;

There are more options in select command not included in this tutorial. This is the statement rules for select statement;

Syntax :

SELECT columns FROM table_name [WHERE condition] [ORDER BY order_type] [LIMIT limit_criteria];

In this tutorial create 1 file (select.php)


select.php

This file will show you how to use the select statement and output the records with while loop in PHP. You must to put some records to table "phonebook" before. If you haven't done, go to Insert Record.

select.php
<?
// Connect database
include("connectdb.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from phonebook");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */

while($row=mysql_fetch_assoc($result)){

// Output
echo "ID : $row['id'] <br/>";
echo "Name : $row['name'] <br/>";
echo "Email : $row['email'] <br/>";
echo "Tel : $row['tel'] <hr>";
}

mysql_close();
?>

Browse "select.php" by go to http://localhost/select.php.

Result :

ID : 1
Name : Jack
Email : jack@abcd.com
Tel : 12345678


ID : 2
Name : Joe
Email : joe@abcd.com
Tel : 87654321

...

Advertise
Put Ebay RSS Feeds onto your website
SEO Elite Software
Domain Dashboard CPanel & Seo Manager
Work at Home Ideas and Opportunities
Introduction & Get Start
Using Form
PHP
PHP Tags
PHP Variables
PHP Control Structures
MySQL
Create Database & Table
Database Connection
Insert Record
Select Record
Update Record
Delete Record

PHPsimple.net
info@phpsimple.net