Im having a problem with this.
I want to echo out the number of post from every city in N.C
Example:
Wilmington (24) <--*24 is the number of post.
Charlotte (35)
Raleigh (15)

include "connect.php";
$wil ='Wilmington';
$cnt = mysql_num_rows(mysql_query("SELECT add_city FROM dbAdd WHERE $add_city='$wil' "));
echo $cnt;

This is the database.

CREATE TABLE `dbAdd` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`add_user` TEXT NOT NULL ,
`add_city` VARCHAR( 100 ) NOT NULL
) ENGINE = MYISAM ;

Recommended Answers

All 4 Replies

Your query seems to be incorrect. You should probably not use WHERE $add_city but WHERE add_city (without the $).

Besides that, a COUNT() query might be more efficient here. E.g.:

<?php
$q = 'SELECT COUNT(add_city) AS c FROM dbAdd WHERE add_city = "' . mysql_real_escape_string($wil) . '"';
$rq = mysql_query($q);
$fetch = mysql_fetch_assoc($rq);
$count = $fetch['c'];

That worked. Thanks!
Now i want to view the posts only from Wilmington when i click on the link.

This is my wil.php

$wil ='Wilmington';
$add_posts_query = mysql_query("SELECT * FROM dbAdd ORDER BY add_city='$wil' DESC LIMIT 200");

Its seems not to echo out the Wilmington post.
It echos out all the post from all the cities.
I only want the Wilmington posts.

What about modifying your query just a little bit, to:

SELECT * FROM dbAdd WHERE add_city = '" . mysql_real_escape_string($wil) . "' ORDER BY add_city DESC LIMIT 200

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.