In looking for one or more data in the database, you can use syntax such as: SELECT field1,field2,… FROM name_table WHERE condition1,condition2,…ORDER BY name_field
If field displayed is all field from the table, so all of the name field itself does not have to be declared but it is enough to change with the sign * then all field will be accessed.
ORDER BY parameter shows the data that is organized based on which field you choose. The default sequence is from the smallest one (number sequence), from A-Z (letter sequence), and from the first data to the last data (time sequence).
You can reverse these sequence by adding DESC attribute. For example, we will search all of the database data of data_employees and show it based on the name field.
<html>
<head>
<title>Search data</title>
</head>
<body>
<table>
<tr>
<td align="center">EMPLOYEES DATA</td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td>NAME</td>
<td>EMPLOYEES<br>NUMBER</td>
<td>ADDRESS</td>
</tr>
<?
//the example of searching data
with the sequence based on the field name
//search.php
mysql_connect("localhost","root","");//database connection
mysql_select_db("employees");

$order = "SELECT * FROM data_employees ORDER BY name";
//order to search data
//declare in the order variable

$result = mysql_query($order);
//order executes the result is saved
//in the variable of $result

while($data = mysql_fetch_row($result)){
echo("<tr><td>$data[1]</td><td>$data[0]</td><td>$data[2]</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>

mysql_fetch_row() Function

Mysql_fetch_row() function takes the data from $result variable in per line. The first take is the top-ranking data line. The data that it takes is in the array shape where the element from array is the field of data table.
For example, in the program of data_employees, the syntax of $row = mysql_fetch_row($result) will produce:
<?php
//the example of searching data
//with the sequence based on the field name
//search.php
mysql_connect("localhost","root","");//database connection
mysql_select_db("employees");

$order = "SELECT * FROM data_employees ORDER BY name";
//order to search data
//declare in the order variable

$result = mysql_query($order);

//order executes the result is saved
//in the variable of $result

while($data = mysql_fetch_row($result)){
print_r($data);
}
?>
Output: Array ( [0] => 1 [1] => Masud Alam [2] => Dhaka ) Array ( [0] => 2 [1] => Sohel Alam [2] => Bangladesh ) And so on until the while order get the false value then the restarting will be stopped.

mysql_fetch_array() Function

Beside using mysql_fetch_row() function in order to get the query result into database, you can also use mysql_fetch_array() function. It has the same function as mysql_fetch_row() function where the data reads line per line.
The difference of both function is that the result of mysql_fetch_array() function is in the array assosiatif shape. For example, if you use mysql_fetch_array() in the program of data_employees such as $row = mysql_fetch_array($result) will produce:
<?php
//the example of searching data
//with the sequence based on the field name
//search.php
mysql_connect("localhost","root","");//database connection
mysql_select_db("employees");

$order = "SELECT * FROM data_employees ORDER BY name";
//order to search data
//declare in the order variable

$result = mysql_query($order);

//order executes the result is saved
//in the variable of $result

while($data = mysql_fetch_array($result)){
print_r($data);
}
?>
Output: Array ( [0] => 1 [employees_number] => 1 [1] => Masud Alam [name] => Masud Alam [2] => Dhaka [address] => Dhaka ) Array ( [0] => 2 [employees_number] => 2 [1] => Sohel Alam [name] => Sohel Alam [2] => Bangladesh [address] => Bangladesh )

0 comments:

Post a Comment

 
Top
Blogger Template