Collections:
Looping through Query Output Rows in MySQL
How To Query Tables and Loop through the Returning Rows in MySQL?
✍: FYIcenter.com
The best way to query tables and loop through the returning rows is to run the SELECT statement with the mysql_query() function, catch the returning object as a result set, and loop through the result with the mysql_fetch_assoc() function in a while loop as shown in the following sample PHP script:
<?php
include "mysql_connection.php";
$sql = "SELECT id, url, time FROM fyi_links";
$res = mysql_query($sql, $con);
while ($row = mysql_fetch_assoc($res)) {
print($row['id'].",".$row['url'].",".$row['time']."\n");
}
mysql_free_result($res);
mysql_close($con);
?>
Using mysql_fetch_assoc() is better than other fetch functions, because it allows you to access field values by field names. If you run this script, you will see all rows from the fyi_links table are printed on the screen:
101, dev.fyicenter.com, 2006-07-01 22:29:02 102, dba.fyicenter.com, 2006-07-01 22:29:02 1101, dev.fyicenter.com, 2006-07-01 22:29:02 1102, dba.fyicenter.com, 2006-07-01 22:29:02
Don't forget to call mysql_free_result($res). It is important to free up result set objects as soon as you are done with them.
⇒ Breaking Query Output into Pages in MySQL
⇐ What Is a Result Set Object in MySQL
2017-06-28, 8143🔥, 0💬
Popular Posts:
How To List All Login Names on the Server in SQL Server? If you want to see a list of all login name...
How To Convert Numeric Values to Integers in SQL Server Transact-SQL? Sometimes you need to round a ...
What Privilege Is Needed for a User to Delete Rows from Tables in Another Schema in Oracle? For a us...
How To Install Oracle Database 10g XE in Oracle? To install 10g universal edition, double click, Ora...
How To Install PHP on Windows in MySQL? The best way to download and install PHP on Windows systems ...