PHP/MySQL Tutorial Part 4 – Retrieve Data
To retrieve after inserting them (see older post PHP/MySQL Tutorial Part 3), simply select the record from the desired table. Use this SQL statement:
SELECT * FROM table_name;
Eg.
$result = mysql_query(“SELECT * FROM tablename”);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_assoc($result))
{
echo $row['name'];
echo “-”;
echo $row['email'];
}
This would display:
Terence – terence@someemail.com
Johnny – johnny@otheremail.com
You can control the results with varying the statement. Eg.
SELECT name FROM tablename – retreives the name column only
SELECT * FROM tablename ORDER BY id DESC – shows last record first
SELECT * FROM tablename LIMIT 5 – shows the first 5 results only







