Need unique url but require non unique information from other columns I am making a review system and I need to be able to only select the DISTINCT url from the database, but then pull the relevant page title and rating time. I have managed to get it working by using:

&lt;?php
//opens conection
include 'conn.php';

echo $_COOKIE["search"];

$sql = "SELECT * FROM feedback WHERE url LIKE '%$_COOKIE[search]%' GROUP BY url ORDER   BY ratingtime DESC";

//runs the query
$query = mysql_query($sql);
$linecount = 1;

//populates the table with the information
echo '&lt;table id="myTable" class="tablesorter"&gt;';
echo'&lt;thead&gt;
    &lt;tr class ="null"&gt;
      &lt;th&gt;URL&lt;/th&gt;
      &lt;th&gt;Page Title&lt;/th&gt;
      &lt;th&gt;Date Last Modified&lt;/th&gt;
      &lt;th&gt;Average Rating&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;';
echo '&lt;tbody&gt;';
echo '&lt;tr&gt;';

while($row = mysql_fetch_array($query))
{
  echo '&lt;td class="overFlow"&gt;&lt;a href="extra_info.php?url='.$row['url'].'"&gt;' .$row['url']. '&lt;/a&gt;&lt;/td&gt;';
  echo '&lt;td&gt;&lt;a href="extra_info.php?url='.$row['url'].'"&gt;' . $row['pagetitle'] . '&lt;/a&gt;&lt;/td&gt;';
  echo '&lt;td&gt;' . $row['ratingtime'] . '&lt;/td&gt;';

  $check = mysql_query("SELECT `rating` FROM feedback WHERE url = '$row[url]'");
  $totalvotes = mysql_num_rows($check);
  $result = mysql_query("SELECT SUM(rating) AS 'rating_total' FROM feedback WHERE url = '$row[url]'") or die(mysql_error());
  $row = mysql_fetch_assoc($result);
  $add = $row['rating_total'];
  $average = $add / $totalvotes;

  echo '&lt;td&gt;' .round($average, 2). '&lt;/td&gt;';
  echo '&lt;/tr&gt;';

  $linecount++;
}

echo '&lt;/tbody&gt;';
echo "&lt;/table&gt;";
?&gt;


I just wondered if there was a much better way to go about doing this? 
I am a complete noob when it come to php so if you provide any advice could you please provide evidence/documentation so that I can read up on it and learn.
I would do it like this

&lt;?php
#opens conection
include 'conn.php';

echo $_COOKIE["search"];


$search = mysql_real_escape_string($_COOKIE[search]);
$sql    = "SELECT * FROM feedback WHERE url LIKE '%{$search}%' GROUP BY url ORDER BY ratingtime DESC";
#runs the query
$query = mysql_query($sql);
$linecount = 1;

#populates the table with the information
echo '&lt;table id="myTable" class="tablesorter"&gt;';
echo '&lt;thead&gt;
        &lt;tr class ="null"&gt;
          &lt;th&gt;URL&lt;/th&gt;
          &lt;th&gt;Page Title&lt;/th&gt;
          &lt;th&gt;Date Last Modified&lt;/th&gt;
          &lt;th&gt;Average Rating&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;';

while($row = mysql_fetch_array($query)){

        $sqlurl = mysql_real_escape_string($row['url']);

        $feedback_query = mysql_query("SELECT SUM(`rating ) / COUNT(`rating`) AS avg_rating FROM feedback WHERE url = '{$sqlurl}'");
        $feedback       = mysql_fetch_assoc($feedback_query);

        $encodedurl = urlencode($row['url']);

        echo "&lt;tr&gt;
                &lt;td class'overFlow'&gt;&lt;a href='extra_info.php?url={$encodedurl}'&gt;{$row['url']}&lt;/a&gt;&lt;/td&gt;
                &lt;td&gt;&lt;a href='extra_info.php?url={$encodedurl}'&gt;{$row['pagetitle']}&lt;/a&gt;&lt;/td&gt;
                &lt;td&gt;{$row['ratingtime']}&lt;/td&gt;
                &lt;td&gt;".round($feedback['avg_rating'], 2)."&lt;/td&gt;
              &lt;/tr&gt;";
        $linecount++;
}

echo '&lt;/tbody&gt;
      &lt;/table&gt;';
?&gt;


If improved your query to get the Avg. Rating and added mysql_real_escape_string to prevent the mysql injection vulnerability.