Jump to content

query help


knaj11

Recommended Posts

my goal is to query artist name from db which that is starting with a certian first letter sample if L, queries lady gaga, lady antebellum and etc...each artist name contains different songs. lady gaga has bad romance, pokerface, and etc...

 

now my problem is that it my query repeats lady gaga since it has two songs...

considering my db table has id, artist, title, content

sample db datas:

1  lady gaga  bad romance  content

2  lady gaga    poker face      content

3  lady antebellum    idk        content

 

my query would result

lady gaga

lady gaga

lady antebellum

 

having a hard time to adjust this one to not query repeating artist...

 

$query = "select id, artist from lyrics where artist like 'a%' order by artist ASC";
		$result = mysql_query($query) or die ("Error in query: $query . " . mysql_query());

		if (mysql_num_rows($result) > 0)
		{
			while ($row = mysql_fetch_array($result))
			{
			$id = $row['id'];
			$artist = $row['artist'];
		?>
			<li><b><a href="content.php?id=<?php echo $id; ?> "><?php echo $artist . ' lyrics'; ?></a></b><br>
		<?php
			}
		}
		else
		{
			echo 'No Content';
		}

thanks in advance for helping... :D

Link to comment
Share on other sites

problem with distinct here is that im selecting the id and artist in the db table...

see code above...id here is auto increment.

That makes no sense -- if you have two artists repeated, that's due to having 2 sets of lyrics -- that's not WRONG.

Link to comment
Share on other sites

Think about what your asking for, only one row to be returned per artist, but you want the id per matched row, this isn't possible. As fenway mentioned the results you are getting are correct. You simply need to filter it when outputting it with PHP.

 

$cur_artist = '';
while( $row = mysql_fetch_assoc( $result ) ) {
   if( $cur_artist != $row['artist'] ) {
      $cur_artist = $row['artist'];
      echo $cur_artist . '<br/>';
   }
   echo $row['id'];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.