-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;
3

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

echo count($result); always echo "1". And sorry, i do not understand, where to put that 2nd code ;/
@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?
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

1 Comment

did you try this ?@Rahul Gupta

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.