-2

This is my code:

$sql = "SELECT count name_id, name FROM TABLE WHERE tag1 in ('$tag1') OR tag2 in ('$tag1') $andor tag2 in ('$tag2') OR tag1 in ('$tag2') ORDER BY rating desc LIMIT 5 OFFSET 0";
$result = $conn->query($sql);

And now, I want to echo the number of name_id. So, what to do?

Make new line something like this: $sql = "SELECT count(name_id)

echo $sql;
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rahul Gupta
  • 1
  • 1
  • 2

2 Answers2

1

If you want count the no of rows in the results

echo  count($result);

if you want retrive it using SQL

SELECT COUNT(*) FROM table_name;

You can include your conditions to this query and get the count of filtered data. Read this for your reference on SQL count

Kuru
  • 738
  • 5
  • 18
  • echo count($result); always echo "1". And sorry, i do not understand, where to put that 2nd code ;/ – Rahul Gupta May 18 '17 at 03:48
  • @RahulGupta that mean it's retrieving only one row. check the answer for reference, DO you want retrieve only the count or the data also? – Kuru May 18 '17 at 03:55
0

Try below:

select count(name_id) as namecount FROM TABLE WHERE tag1 in ('$tag1') OR tag2 in ('$tag1') $andor tag2 in ('$tag2') OR tag1 in ('$tag2') group by name_id ORDER BY rating desc LIMIT 5 OFFSET 0

you need to use group by

lalithkumar
  • 3,480
  • 4
  • 24
  • 40