PHP MySQL Query Multiple Records
From w3cyberlearnings
Contents |
PHP MySQL Select All Records
Select all records from a table in a single query can be very memory consuming. In order to select all records in a table can be very easy. You call the SELECT Statement without using the WHERE Statement.
Syntax
$sel_query = 'SELECT first_name, last_name FROM student'; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo '<table border="1">'; echo '<tr><th>First Name</th><th>Last Name</th></tr>'; while ($row = mysql_fetch_assoc($result)) { echo '<tr>'; foreach ($row as $value) { echo '<td>' . $value . '</td>'; } echo "</tr>"; } echo '</table>';
Note
Select all records at once can be very bad for your server memory, and it can be very slow. You can use Pagination for solution.
Example 1
<?php define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $connection = mysql_connect(HOST, USER, PASS); if (!$connection) { die("can not connect to the server!<br/>"); } else { $rdb = mysql_select_db(DBNAME); if (!$rdb) { die("The " . DBNAME . "database could not be selected"); } else { $sel_query = 'SELECT first_name, last_name FROM student'; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo '<table border="1">'; echo '<tr><th>First Name</th><th>Last Name</th></tr>'; while ($row = mysql_fetch_assoc($result)) { echo '<tr>'; foreach ($row as $value) { echo '<td>' . $value . '</td>'; } echo "</tr>"; } echo '</table>'; } } mysql_close($connection); ?>