![]() |
|
PHP MySQL SELECT Statement. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | MySQL SELECT Statement |
|
|
SELECT column_name(s) FROM table_name We have to use PHP mysql_query() function to execute the above statement. Let's see an example: Example The following example selects all the data stored in the "Students" table. Note that the * character is used to select all the data in the table: <?php $con = mysql_connect("localhost", "adam", "ada"); if (!$con) { die('Error connecting database'); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Students "); while($row = mysql_fetch_array($result)) { echo $row['name'] . " " . $row['address']; echo "<br />"; } mysql_close($con); ?> The above example stores the data returned by the mysql_query() function in the $result variable. It, then uses the mysql_fetch_array() function in the while loop to display that data. Please note that the first call to mysql_fetch_array() returns the first record, second call to the function returns the second record and so on. To print the value of each row, we use the PHP $row variable ($row['name'] and $row['address']).
Next: PHP MySQL Update
|