I'm working for a page where it shows a tables from database. Let's say I have 100 rows. How to show 21st-30th row?
In the table, there's some fields, not only email, name, and description. So, I used mysqli_fetch_assoc
for($num=21;$num<=30;$num++){
$row=mysqli_fetch_assoc($result);
echo "<tr>";
echo "<td>".$num."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['description']."</td>";
echo "</tr>";
}
The $num
shows the desired number but the table itself shows the data from 1st-10th. I want it shows the data from 21st-30th
You can already fix your code with sql queries (for faster performance) adding LIMIT 20, 10 (get 10 records beginning with row 21)
but if you have other logic to be applied on your code (as if your displaying dynamic records)
$row=mysqli_fetch_assoc($result);
for($num=0; $num<=$result->num_rows; $num++) {
if($num>=21 && $num<=30){
echo "<tr>";
echo "<td>".$num."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['description']."</td>";
echo "</tr>";
}
}
other thing you might consider is using foreach
$row=mysqli_fetch_assoc($result);
$num = 0;
foreach (range(21, 30) as $row) {
$num++;
echo "<tr>";
echo "<td>".$num."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['description']."</td>";
echo "</tr>";
}
Please, try with sql query, where you defined limit of rows like start from row no to end of row no. I wrote one sql query at bellow:
Syntax:
SELECT column1, column2, ...
FROM table_name
LIMIT offset, count;
Example:
SELECT * from MYTABLE LIMIT 20,10;
Two possibilites you can doing this way
First
SELECT * from MYTABLE LIMIT 20, 10
Second
// 0,1,2,3....19 19 is record of 20
for($num=19;$num<=29;$num++){
}
You may try like below.
SELECT * FROM Tablename WHERE id BETWEEN 21 AND 30
Where, id -> Will be the unique key of the table.
Thanks.
Google PHP Api Client - I keep getting Error 401: UNAUTHENTICATED
Getting data from nested fields using mongoDB and php driver manager
How to update photo in Active Directory using PHP ldap_modify
Turning off multiple WordPress menu items for email addresses
I placed the phpseclib files in my application/third_party folder, I have included the path in my controller as shown below
I am write code in for loop , for multiple insert value using codeigniter framework
I am doing query on view with single predicates which gives me the record in 4-7 seconds, but when i try to retrieve the record with same predicate and directly with underlying query from that view it gives me records in less then secondsI am using MySQL
I have table in SQL