Jump to content

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
https://forums.phpfreaks.com/topic/199227-query-help/
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
https://forums.phpfreaks.com/topic/199227-query-help/#findComment-1048439
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.