|
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 "
Firstname |
Lastname | Age |
"; while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['FirstName'] . " | ";
echo " " . $row['LastName'] . " | "; echo "" . $row['Age'] . " | "; echo " "; } echo " "; mysql_close($con);
?> |