I have a MySQL table having many rows and my question is,
How to
i) Display rows from MySQL table as the row length is equal to multiples of 3
ii) In other case if row count not equal to multiples of 3 then display the row equal to nearest multiples of 3 for example, if the row length is 5 then display only first 3 rows.
for more clarification,
Use
$res=mysqli_query(
$con,"SELECT * FROM tbl_name");
$row=mysqli_num_rows($res);//calculating total row count
$lim=$row-$row%3;//deciding limit
$res1=mysqli_query(
$con,"SELECT * FROM tbl_name LIMIT $lim");
//echo '<br>'.$row1=mysqli_num_rows($res1);
while($show=mysqli_fetch_array($res1))
{
echo '<br>'.$show['id'];
}
Execute the query & show your data
SELECT (COUNT(*)-(COUNT(*)%3)) FROM tbl_name INTO @lim;
SELECT @rownum := 0;
SELECT t.*, @rownum := @rownum + 1 AS rowno FROM tbl_name t WHERE rowno <= @lim;
$res=mysqli_query( $conn, "SELECT COUNT(*) AS cnt FROM tbl_name");
$rec=mysqli_fetch_array($res);
$lim=$rec["cnt"]-($rec["cnt"]%3);
$res=mysqli_query( $conn, "SELECT * FROM tbl_name LIMIT $lim");
You'll need to validate cnt
and lim