Reputation: 33
Trying to get total count of rows using variable in sql query but not able to get that can anyone here help me with this ?
<?php
$business = $value['business_name'];
//echo $business;
$sql = "SELECT COUNT(*) FROM listings_reviews WHERE business_name = '$business'";
$result = $conn-> query($sql);
$row = $result -> fetch_assoc();
//print_r ($row) ;
$rating = implode($row);
echo $rating;
?>
Upvotes: 0
Views: 529
Reputation: 10216
Give an alias to your COUNT(*)
in the SQL, for example, here I have aliased it to cnt
.
Then in PHP you can identify clearly the variable of the $row
array
$business = $value['business_name'];
//echo $business;
$sql = "SELECT COUNT(*) AS cnt FROM listings_reviews WHERE business_name = '$business'";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$rating = $row["cnt"];
}
echo $rating;
Upvotes: 2
Reputation: 9582
Group by business name:
$business = $value['business_name'];
$sql = "SELECT COUNT(*) AS c FROM listings_reviews WHERE business_name = '$business' GROUP BY business_name";
$result = $conn->query($sql);
$row = $result ->fetch_assoc();
$listingsByBusiness = $row['c'];
For reference, see
Upvotes: 0
Reputation: 99
Like this:
$q = 'SELECT COUNT(*) AS `all_lines` FROM `table_name`';
$record = $db->query_first($q);
$all_lines = $record['all_lines'];
Upvotes: 0