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
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.