PHP Programing language

PHP MySQL Select
Previous Home Next

As you know there are two types of MySQL queries

The queries which we using to create a table and the query we used to insert data into our newly created table. In the query in this lesson is SELECT, which is used to get information from the database, so that its data can be used in our PHP script.

How to retrieve information from MySQL

Finally, we getting to use the data in our MySQL database to creating a dynamic PHP page. In this example we will select everything in our table "tablefirst" and put it into a nicely formatted HTML table. Remember, if you don't understand the HTML or PHP code, be sure to check out the HTML and/or PHP Tutorial(s).

How to connect into PHP & MySQL Code:

mysql_select_db("test") or die(mysql_error());

// Get all the data from the "tablefirst" table
$result = mysql_query("SELECT * FROM tablefirst") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table

echo "<tr><td>"; 
echo $row['name'];
echo "</td><td>"; 
echo $row['age'];
echo "</td></tr>"; 
} 
echo "</table>";
?>

Output:

Name Age
Amit 23
Akash 21
Abinav 15

 

In your table we only had three entries in table, three rows appeared above. If you want to add more entries to your database's table, then you would attach in table each additional row, into the table. If you do not understand the above PHP, you can view our PHP Array Tutorial & PHP Loop Tutorial.

$result = mysql_query...

When you selecting the items from a database using mysql_query, then the data is returned as a MySQL result. Since we want to use this data in our table we need to store it in a variable. $result now holds the result from our mysql_query.

SELECT * FROM tablefirst

In English, this line of code reads "Select everything from the table example". The asterisk is the wild card in MySQL which just tells MySQL to retrieve every single field from the table.

while($row = mysql_fetch_array( $result )

The mysql_fetch_array function gets the next-in-line associative array from a MySQL result. By putting it in a while loop it will continue to fetch the next array until there is no next array to fetch. This function can be called as many times as you want, but it will return FALSE when the last associative array has already been returned.

By placing this function within the conditional statement of the while loop, we can kill two birds with one stones.

We can retrieve the next associative array from our MySQL Resource, $result, so that we can print out the name and age of that person. We can tell the while loop to stop printing out information when the MySQL Resource has returned the last array, as False is returned when it reaches the end and this will cause the while loop to halt.

In our MySQL table "example" there are only two fields that we care about: name and age. These fields are the keys to extracting the data from our associative array. To get the name we use $row ['name'] and to get the age we use $row ['age'].

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: