0

如何转换为使用数据库计数?

function get_orders_count(){                                                                    
  global $mysqli;                                                                        
  $query ='                                                                                      
    SELECT                                                                                 
      a.order_id                                                                     
    FROM                                                                                   
      assignments a                                                                  
    WHERE                                                                                  
      client_id = '.$_SESSION['user']['ID'];                                         
  $result = $mysqli->query($query);                                                              
  return $result->num_rows;                                                                      
}
4

1 回答 1

2

使用COUNTSQL 函数。

function get_orders_count(){                                                                    
  global $mysqli;                                                                        
  $query ='                                                                                      
    SELECT                                                                                 
      COUNT(a.order_id)                                                                     
    FROM                                                                                   
      assignments a                                                                  
    WHERE                                                                                  
      client_id = '.$_SESSION['user']['ID'];                                         
  $result = $mysqli->query($query);                                                              
  return $result->$result->fetch_row()[0];                                                                      
}

如果order_id是主键使用

COUNT(*)
于 2013-11-01T05:29:23.657 回答