简体   繁体   中英

how to get the number of rows in a table that come after a row with a specific id?

how do i get the number of rows in an sql table that come after a row with a specific id with php? I want to get the number of rows that come after the row with an id of 6.

Select count(*) from TableName where ID > 6

The SQL for a query to count the rows with an ID greater than 6, assuming your table is named table and the ID column is named id will be:

SELECT count(*) FROM table WHERE id > 6;

To do this from PHP, you can modify the example in the docs to add your own query. The output could also be tweaked to return a scalar value.

<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT count(*) FROM table WHERE id > 6';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM