简体   繁体   中英

mysql if num rows more than 0 show results else echo no recent activity - not working?

I am trying to get this code to work. I am getting a list of results from my database which are less than 30 days old. And displaying them in my div.

This works fine, however if there are no results I want to echo out No Recent Activity, and I am unable to get this bit to work. I am using a MySQL num row count and else statement to try and get it to do what I need but It won't show no recent activity even if there is no results.

Please can someone show me where I am going wrong.

thanks,

Code:

<div class="dashboard_stats">
<?php require_once 'config.php'; ?>

<?php
$table = 'recent_activity';


echo '<div class="dashboard_stats_heading"><table class="table_other"><tr><td>Description</td><td>Reference</u></td><td>Status</td><td>Supplier Name</td></tr></table></div>';

$query1 = "SELECT * FROM $table WHERE user_id = '{$_SESSION['id']}' AND date > NOW() - INTERVAL 30 DAY ORDER BY date DESC";
$result1 = mysql_query($query1);
while ($row1 = mysql_fetch_array($result1)) {


    if(mysql_num_rows($result1)>0){

    $date = $row1['date'];
    require_once 'time_ago.php';

    if($row1['activity_type']==='Bank'){
        $activity_type = 'Bank Details Changed';
    }else{
        if($row1['activity_type']==='Username'){
            $activity_type = 'Username Changed';   
        } else {
            if($row1['activity_type']==='Password'){
                $activity_type = 'Password Changed';   
            } else {
                if($row1['activity_type']==='Contact'){
                    $activity_type = 'Contact Details Changed';   
                } else {
                    if($row1['activity_type']==='Invoice'){
                        $activity_type = 'Invoice Submitted';   
                    } else {
                        if($row1['activity_type']==='Trading'){
                            $activity_type = 'Trading Suspended';     
                        }
                    }
                }
            }
        }
    }




        echo '<table><tr><td><p>'.$activity_type.'</p></td><td><p>1234</p></td><td><p>'.$row1['status'].'</p></td><td><p>'; 
        echo pretty_relative_time($row1['date']);
        echo '</p></td></tr><tr class="separator"></tr></table>';

    }else{

        echo   '<div id="content">No Recent Activity</div>';   

    }
}

echo '</div>';
?>

</div>

You need to move the if statement out of the while loop as its not being run if theres nothing for the while loop to iterate over.

switch the if and while around

if(mysql_num_rows($result1)>0){

while ($row1 = mysql_fetch_array($result1)) {

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