计算MYSQL DB的结果数

时间:2017-12-23 10:34:41

标签: php mysql codeigniter

我想知道从mysql返回的结果数量的正确方法。

这是我从表中获取数据的代码。

$bids = $this->db->query("SELECT bidID,listingID, listing_title, bid_date, username, vintage, starting_, bin,
amount, sold, sold_date, bid_type 
FROM bids

JOIN listings ON listingID = bid_listing
JOIN users ON list_uID = userID

ORDER BY bidID DESC");

$bids = $bids->result();
$data['bids'] = $bids;

然后我运行foreach来打印数据。例如:

  <?$i = "0"; foreach ($bids as $l): ?> <? echo $l->listing_title;?> 

我想在商品目录后添加出价/结果的数量。

由于

4 个答案:

答案 0 :(得分:0)

您可以针对您的问题尝试此解决方案:

$bids = $this->db->query("SELECT bidID,listingID, listing_title, bid_date, username, vintage, starting_, bin,
amount, sold, sold_date, bid_type 
FROM bids

JOIN listings ON listingID = bid_listing
JOIN users ON list_uID = userID

ORDER BY bidID DESC");

$bids = $bids->result();
$no_0f_bids = $bids->num_rows();
$data['bids'] = $bids;
$data['no_0f_bids'] = $no_0f_bids;

然后,您可以在视图文件中打印任何出价。

<?php  
echo "No of Bids : ".$no_0f_bids;
foreach ($bids as $l):
    echo $l->listing_title;
endforeach;
?> 

您可以访问Generating codeigniter Query Results.

的此链接

我希望它会有所帮助。

答案 1 :(得分:0)

试试这个:

$bids = $this->db->query("SELECT SQL_CALC_FOUND_ROWS bidID,listingID, listing_title, bid_date, username, vintage, starting_, bin, amount, sold, sold_date, bid_type FROM bids JOIN listings ON listingID = bid_listing JOIN users ON list_uID = userID ORDER BY bidID DESC");

$total = $this->db->query("SELECT FOUND_ROWS() as total");
$data['bids'] = $bids->result();
$data['bids_total'] = $total->row()->total;

或者

$bids = $this->db->query("SELECT  bidID,listingID, listing_title, bid_date, username, vintage, starting_, bin, amount, sold, sold_date, bid_type FROM bids JOIN listings ON listingID = bid_listing JOIN users ON list_uID = userID ORDER BY bidID DESC");

$data['bids'] = $bids->result();
$data['bids_total'] = $bids->num_rows();

或者

$data['bids_total'] = count($data['bids']);

答案 2 :(得分:0)

你应该有if ($dafter = mysqli_prepare($conn, "INSERT INTO tb_dafter VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) { mysqli_stmt_bind_param($dafter, "ssssssss", $_POST['id'], $_POST['nama'], $_POST['asal_sekolah'], $_POST['jenis_kelamin'], $_POST['nama_ayah'], $_POST['nama_ibu'], $_POST['alamat'], $_POST['no_hp']); mysqli_stmt_execute($dafter); $pesan1 = "Berhasil daftar"; echo "<script type='text/javascript'>alert('$pesan1');</script>"; } else { $pesan2 = htmlentities(mysqli_error($conn)); echo "<script type='text/javascript'>alert('$pesan2');</script>"; } 此函数将是您想要的返回计数。

答案 3 :(得分:0)

尝试使用codeigniter活动记录

public function count() {
  $this->db->select('*');
  $this->db->from('bids');
  $this->db->join('listings', 'listings.listingID = bids.bid_listing', 'LEFT');
  $this->db->join('users', 'users.userID = listings.list_uID', 'LEFT');
  $query = $this->db->get();

  return $query->num_rows();
}

echo $this->count();

https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data