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

Home | Tutorials | PHP & MySQL Resources | Web Tools
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

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 : [email protected]
Tel : 12345678


ID : 2
Name : Joe
Email : [email protected]
Tel : 87654321

...


Send Email to Multiple Persons
You can send email to mutiple persons in one time. This tutorial shows you in 1 file with a normal form. When you submit, this file will be sent an email immediately to the target emails from your database.
Get Last ID after Insert
You can get the ID (AUTO_INCREMENT column as primary key) generated after you insert a new record in your table by a simple function "mysql_insert_id()".


PHPsimple.net
[email protected]