Select Statement (MYSQL)

|

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 BYCriteria for ordering the records.
LIMITLimit the number of records returned.

Syntax:

SELECT column_name(s)
FROM table_name

< ? php

$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

}
mysql_select_db ("mydb", $con);

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

echo "< border="'1'" align="’center’">

<>

<><> Firstname < / b >

<><>Lastname< / b >

<><>Age< / b >< / td >

< / tr >";
while($row = mysql_fetch_array($result))
{
echo "<>";
echo "<>" . $row['FirstName'] . "< / td >";

echo "<>" . $row['LastName'] . "< /td >";

echo "<>" . $row['Age'] . "< / td >";

echo "< / tr >";

}
echo "< / table >";


mysql_close($con);

?>