check if the query results empty row mysqli

前端 未结 2 1231
一向
一向 2020-11-29 11:17

I am using this code, but I don\'t understand how to check if the query returns zero rows. How can I check that?

$results = $mysqli->query(\"SELECT ANNOUN         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 11:30

    You can use the num_rows on the dataset to check the number of rows returned. Example:

    $results = $mysqli->query("SELECT ANNOUNCE_NUMBER,ANNOUNCEMENTS,ANNOUNCE_TYPE,POST_DATE FROM home WHERE ANNOUNCE_NUMBER NOT LIKE $excludewelcome AND ANNOUNCE_NUMBER NOT LIKE $excludenews ORDER BY ANNOUNCE_NUMBER DESC LIMIT $position, $items_per_group");
    if ($results) { 
    
        if($results->num_rows === 0)
        {
            echo 'No results';
        }
        else
        {
            //output results from database
            while($obj = $results->fetch_object())
            {
                if($obj->ANNOUNCE_TYPE=='NEWSEVENTS')
                {
                    $realstring='News and Events';
                }
                else
                {
                $realstring='Welcome Note';
                }
    
                echo '
  • '.$realstring.''; echo '
    '; echo '('.$obj->POST_DATE.' ) '.$obj->ANNOUNCEMENTS.'
  • '; } } }

提交回复
热议问题