0

SQl query running to return the total count from the query.

this code works when running SQL with PHPmyAdmin

But on the page it is not displaying echo of the count ?

Not sure if I could have overlooked something here.

Many Thanks!

$sql2=mysql_query("SELECT count(*) 
FROM main_table LEFT JOIN houses ON main_table.housenumber = houses.housenumber AND main_table.streetname = houses.streetname
WHERE main_table.city='1'
group by main_table.city ORDER BY average DESC, houseID DESC, reviewID DESC;");

while($row=mysql_fetch_array($sql2))
{
    $count=$row['count'];
    echo $count;;
}

4 Answers 4

1

Try this ....

$sql2=mysql_query("SELECT 
      COUNT(*) AS count
    FROM
      main_table 
      LEFT JOIN houses 
        ON main_table.housenumber = houses.housenumber 
        AND main_table.streetname = houses.streetname 
    WHERE main_table.city = '1' 
    GROUP BY main_table.city 
    ORDER BY average DESC,
      houseID DESC,
      reviewID DESC") ;

    while($row=mysql_fetch_array($sql2))
    {
        $count=$row['count'];
        echo $count;
    }

You have mistake in your query, you are not adding count in select as aliases, and below in while you are using aliases . Try this.

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

Comments

0

name your column :

...mysql_query("SELECT count(*) as count....

Comments

0

add SELECT count(*) as 'count' from ...

Comments

0

try starting your query with :

SELECT count(*) as count ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.