MySQL Select Query

Select Query Syntax

SELECT * FROM 'database_name' WHERE 'column_1_name' = 'value_1' ORDER BY 'column_1_name' DESC LIMIT 1

To execute MySQL query in PHP is easy, write the following code and replace CODE with your MySQL query:

<?php mysql_query("CODE"); ?>

Select all rows and order

One of the previous articles I showed you how to create a MySQL database. Now I select all rows from the students table and order the result by first_name ascending.

SELECT * FROM 'students' ORDER BY first_name ASC

Result

idfirst_namelast_namegender
1 Jack Daniels male 
2 Lara Croft female
4 Mickey Mouse male
3 Ronald McDonald male

PHP script

<?php
$query = mysql_query("SELECT * FROM `students` ORDER BY `first_name` ASC");
while( $row = mysql_fetch_array($query) ){
 echo $row[id] ." ". $row[first_name] ." ". $row[last_name] ." ". $row[gender] . "<br/>";
}
?>

Result

1 Jack Daniels male
2 Lara Croft female
4 Mickey Mouse male
3 Ronald McDonald male

Select one row

SELECT * FROM 'students' WHERE 'first_name' = 'Ronald'

Result

idfirst_namelast_namegender
3 Ronald McDonald male 

Next to read

Tomorrow I post an article about MySQL Update Query, learn how to update rows in the database.


Go Daddy


Related articles

Email Validation

Email Validation

08/02/2013

Learn how to validate email on client side with HTML5, Javascript and on server side with PHP.

Generate Thumbnail on the Fly with PHP

Generate Thumbnail on the Fly with PHP

04/02/2013

In a previous artice I already showed you how to generate square thumbnails with PHP. Now I show you how to keep the original ratio of the image and how to control the alignment of the square image.

Facebook Upload Photo to User′s Profile

Facebook Upload Photo to User′s Profile

31/01/2013

Learn how to upload a photo to Facebook user′s profile with PHP SDK via Graph API!


Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the lion *


Find us on Facebook

Tags

Get online with the world's Best - GoDaddy.com!

Back to Top