Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 25th, 2005, 12:16 PM
Registered User
Join Date: Apr 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I display search results...

I have a Web site that I am building with PHP pages that are going to retrieve information from a MySQL Database. I want to be able to retrieve data from the "city" field and "count" the total number of rows "$city" is in, while displaying only one instance of the name of the "$city". I want the number of rows displayed in parentheses next to the city. In other words, I want the results to look like the following example where the data will be displayed in a 3-column Table formatted using HTML. What I have so far is at the bottom of this post. Please use it for reference. Upon reply, please be specific as I am a beginner! I can tell you more if you need me to. Thanks! To test the functionality yourself, just go to http://www.businessdirectorylistings.com/tennessee.php. You will see for yourself what kind of results I am currently getting.


Your Search Found The Following Results...

Chicago (3) Houston (2) Nashville (5)
Panama City (4) Phoenix (3) Portland (2)

NOTE: The cities will have hyperlinks that link to a page of search results for that city.


 1. Chicago
 2. Chicago
 3. Chicago
 4. Houston
 5. Houston
 6. Nashville
 7. Nashville
 8. Nashville
 9. Nashville
10. Nashville
11. Panama City
12. Panama City
13. Panama City
14. Panama City
15. Phoenix
16. Phoenix
17. Phoenix
18. Portland
19. Portland


<title>Get My Query</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



$connection = mysql_connect($host,$user,$password)
    or die ("Couldn't connect to server");
$db = mysql_select_db($database,$connection)
    or die ("Couldn't select database");
$state = "$state";
$city = "$city";
$query = "SELECT * FROM tblCities WHERE city='$city' ORDER BY city";
$result = mysql_query($query)
or die ("Couldn't execute query");
$nrows = mysql_num_rows($result);

/* Display results in a table */
echo "<h1>Found The Following Results...</h1>";
echo "<table cellspacing='8'>";
echo "<tr><td colspan='4'></td></tr>";

for ($i=0;$i<$nrows;$i++)


$n = $i + 1; /* Add 1 so that records won't start displaying at 0 */
$row = mysql_fetch_array($result);
echo "<tr>\n
<td><a href='http://www.businessdirectorylistings.com/listingresults.php?city=$city&state=$state' title='Visit $bizname's Web Site'>$bizname</a></td>\n
<td>($phonearea) $phone</td>\n
<td>$city, $state</td>\n
echo "<tr><td colspan='4'></td></tr>\n";


echo "</table>\n";




Reply With Quote
  #2 (permalink)  
Old April 25th, 2005, 04:59 PM
Authorized User
Join Date: Jan 2005
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts

try this:

Select city, count(*) as City_n FROM tblCities GROUP BY city;

Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter double search results ABplace Beginning VB 6 3 June 28th, 2007 08:11 AM
How do I display search results... aspiretech9 PHP How-To 1 April 26th, 2005 10:12 AM
How do I display search results... aspiretech9 PHP Databases 1 April 25th, 2005 12:11 PM
How display search results ? URGENT arulkumar Classic ASP Databases 2 March 4th, 2005 10:26 AM
Customize Search Results bmains ASP.NET 1.0 and 1.1 Professional 4 January 15th, 2004 08:51 AM

All times are GMT -4. The time now is 10:43 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.