php

I try to build a Notification Popup I have for this a script what seems to work but when I add a code I am = getting the numbers 1 2 3 4 from the 4 records from this user-id

I would like to see only the total number (4)

 <?php $q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'"); $rr=mysqli_num_rows($q); if(!$rr) { echo "<h2 style='color:red'>No any notice for You !!!</h2>"; } else { ?> <?php $i=1; while($row=mysqli_fetch_assoc($q)) { echo "<Tr>"; echo "<td>".$i."</td>"; echo "</Tr>"; $i++; } ?> </table> <?php }?>

simply add the echo out of your loop

$i=1;

while($row=mysqli_fetch_assoc($q))
{

$i++;
}
echo "<Tr>";
echo "<td>".$i."</td>";

echo "</Tr>";

Just output the $rr variable, it contains the number of rows.

mysqli_num_rows

UPDATE, example added based on your code:

<?php 
$q=mysqli_query($conn,"select * from notice where user='".$_SESSION['user']."'");
$rr=mysqli_num_rows($q);
if(!$rr)
{
    echo "<h2 style='color:red'>No any notice for You !!!</h2>";
}
else
{
    echo "<h2 style='color:green'>Total Notice for you: $rr</h2>";      
}
?>

Use MySQL's COUNT function..

assuming your user table has a column named id .. try

 $q=mysqli_query($conn,"select COUNT(`id`) as USERS from notice where.... 

Doing this will result in 1 result, so remove your while and just do..

$row=mysqli_fetch_assoc($q);
echo $row['USERS'];

暂无
暂无

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.

Related Question selecting id and count(id) from table in mysql fetch id from session and store into a different table getting id from user table using $_SESSION Get ID from table inside session user id Count entries from second Table that match id from first Table Select columns, count(id) from table and reference table MySQL SELECT COUNT from one table and ID from another? Session variable calls only the last id from mysql table Match Username (field from mysql table) To Session ID In PHP Query: Count id from table where parameter is counted NULL times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM