PHP MySQL Query Use LIMIT
From w3cyberlearnings
Contents |
PHP MySQL LIMIT
Use LIMIT Statement to limit how many records to be return.
Syntax
- LIMIT Start, Total
$sel_query = 'SELECT first_name, last_name FROM student LIMIT 0,3'; $result = mysql_query($sel_query, $connection);
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 LIMIT 0,3'; $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); ?>