MySQL SELECT STATEMENT

Submitted by:David Villa

Date added:09 May, 2011

Category:MySQL

This is a general code to use when selecting an item from a MySQL database. This code isn't considered definitive and can be written in many varied ways.

Note: This code requires you to connect to the MySQL database to work. Also, all field and table names need to be set up to match.

This MYSQL Code using in PHP

Tags: mysql database , select statement

Code Snippet:

//Select all records in a single field
$sql = "SELECT id FROM table_name";

//Select all records in all fields
$sql = "SELECT * FROM table_name";

//Select a limited number of records in all fields
$sql = "SELECT * FROM table_name LIMIT 0,1"; //start from record 0, show 1 record (show first record)
$sql = "SELECT * FROM table_name LIMIT 0,100"; //start from record 0, show 100 records (show first 100)
$sql = "SELECT * FROM table_name LIMIT 10,1"; //start from record 10, show 1 record (show 10th record)

//Select fields depending on variable values (database search)
$sql = "SELECT * FROM table_name WHERE first_name='Peter'"; //(select all Peter's)
$sql = "SELECT * FROM table_name WHERE first_name='Peter' && location='Australia'"; //(select all Peter's AND in Australia)
$sql = "SELECT * FROM table_name WHERE first_name='Peter' || location='Australia'"; //(select all Peter's OR Australians)


//Output result
$result = mysql_query($sql);
while($rows = mysql_fetch_array($result)) {
$my_output_variable = $rows['id'];
}
 
 

Comments