Sunday 23, November 2014
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    About Us    |    Contact Us

Select Statement

The select query is used to retrieve records from a database.

 

SELECT
Retrieves fields from one or more tables.
FROM
Tables containing the fields.
WHERE
Criteria to restrict the records returned.
GROUP BY
Determines how the records should be grouped.
HAVING
Used with GROUP BY to specify the criteria for the grouped records.
ORDER BY
Criteria for ordering the records.
LIMIT
Limit the number of records returned.

 

Syntax:
SELECT column_name(s)
FROM table_name
Example:

 

$con = mysql_connect("localhost:3306","adam","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("mydb", $con);
$result = mysql_query("SELECT * FROM empinfo");
while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'] . “ “ $row[‘Age’];
  echo "
";
  }
mysql_close($con);
?>

Returned data from mysql_query() function will be stored in the result variable. msql_fetch_array() function is used to return the first row from the recordset as an array. The while loop loops through all the records in the recordset and retrieve all the records from the recordset.

 

Following function can be used in the select statement.

Function
Description
AVG()
Returns the average value in a group of records.
COUNT()
Returns the number of records in a group of records
MAX()
Returns the largest value in a group of records.
MIN()
Returns the lowest value in a group of records
SUM()
Returns the sum of a field.

 

 

Display the Result in an HTML Table

 

$con = mysql_connect("localhost:3306","adam","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }

mysql_select_db("mydb", $con);

 

$result = mysql_query("SELECT * FROM empinfo");

 

echo "



";
while($row = mysql_fetch_array($result))
  {
  echo "
";
  echo "
";

  echo "

";

 

  echo "
";
  echo "
";
  }
echo "
FirstnameLastnameAge
" . $row['FirstName'] . "" . $row['LastName'] . "" . $row['Age'] . "
";
mysql_close($con);

?>


 
 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download Road Pricing  Ebook Road Pricing
   
Download Mammoth Myspace Secrets Ebook Mammoth Myspace Secrets
   
Download C++ Essential Ebook C++ Essential
   
Download How to Think Like a Computer Scientist: Learning with Python   Ebook How to Think Like a Computer Scientist: Learning with Python
   
Download The Magic Bullet  Ebook The Magic Bullet
   
     
Studiesinn.com 2014 All Rights Reserved.
 
Website Designed & Developed by TechXprtz