User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,066 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,540 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 728 | Replies: 3
Reply
Join Date: Dec 2007
Posts: 10
Reputation: tie372 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tie372 tie372 is offline Offline
Newbie Poster

Help Overwriting duplicate mysql results

  #1  
Jan 5th, 2008
How would I make it so when I select something from a mysql database, that information isn't stored in the array twice? Here is my code

<?php
include "header.html";
include "db.php";

if($_GET[by]==genre)
{
  $sort = mysql_query("SELECT genre FROM bands ORDER BY genre");
  while($sort2 = mysql_fetch_array($sort))

  
  {

    $sql=mysql_query("SELECT * FROM bands WHERE genre='$sort2[genre]' ORDER BY name");
    $number=mysql_num_rows($sql);
    $sort2[genre]=str_replace("%20", " ", "$sort2[genre]");
    echo $sort2[genre]." (".$number.")<blockquote>";

    while($rows=mysql_fetch_array($sql))
    {
      $name="$rows[name]";
      $name2=str_replace("%20", " ", "$rows[name]");
      echo "<a href=band.php?band=".$name.">".$name2."</a><br>";
    }

    echo "</blockquote>";
  }
}
...

I have two entries with the genre 'metal.' and so when I execute this code, it displays the header 'metal' twice.
Last edited by tie372 : Jan 5th, 2008 at 2:01 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: somewhere in time
Posts: 71
Reputation: TopDogger is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
TopDogger's Avatar
TopDogger TopDogger is offline Offline
Junior Poster in Training

Re: Overwriting duplicate mysql results

  #2  
Jan 5th, 2008
Have you tried using SELECT DISTINCT?

$sort = mysql_query("SELECT DISTINCT genre FROM bands ORDER BY genre");

That will return only one "distinct" row per genre value.

It looks like you are doing things the hard way. You should be looking at pulling all of your data with one hit on the database by joining the tables. If you have 10 genres, the way you are doing things will hit the database 11 times.
Reply With Quote  
Join Date: Dec 2007
Posts: 10
Reputation: tie372 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tie372 tie372 is offline Offline
Newbie Poster

Re: Overwriting duplicate mysql results

  #3  
Jan 5th, 2008
Originally Posted by TopDogger View Post
Have you tried using SELECT DISTINCT?

$sort = mysql_query("SELECT DISTINCT genre FROM bands ORDER BY genre");

That will return only one "distinct" row per genre value.

It looks like you are doing things the hard way. You should be looking at pulling all of your data with one hit on the database by joining the tables. If you have 10 genres, the way you are doing things will hit the database 11 times.



Thanks it worked perfect.
Reply With Quote  
Join Date: Sep 2005
Posts: 674
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 39
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: Overwriting duplicate mysql results

  #4  
Jan 7th, 2008
Originally Posted by tie372 View Post
Thanks it worked perfect.


Since you're obviously selecting all genres, you don't need to SELECT the genres first. You just have to order your results by the genre, and then by the name.

Just use:

$sql=mysql_query("SELECT * FROM bands ORDER BY genre, name");

If there are some bands that do not have a genre, those will also be selected. This may be desired or you can leave them out with a WHERE clause.
$sql=mysql_query("SELECT * FROM bands WHERE genre != '' ORDER BY genre, name");

Other than that, I don't see any reason for selecting genres.

Usually, you'd want to use the index on a separate table to reference it. Like having genre_id and referencing this in bands. This makes the db smaller and more efficient. Thats off topic however...
Last edited by digital-ether : Jan 7th, 2008 at 2:30 am. Reason: code formatting...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC