Jump to content

mySQL query - How to select names with out duplicates


Recommended Posts

I have two things that should show up. Laszlo and Dusty. When I add the 2nd query it only adds the 1st one on the table. Laszlo. Is it conflicting with each other? Any way to get the same result with something else?

 

	
 $query = "SELECT DISTINCT writer FROM data";
$result = mysql_query($query)
	or die ("Can't do anything with the query!");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
extract($row);
	echo"$writer<br>";
$query = "SELECT COUNT(quality) AS twriter FROM data WHERE writer='$writer' AND date BETWEEN '$year-$month-00' AND 			'$year-$month-32' ";
$result = mysql_query($query)or die(mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
	{
extract($row);
echo"$writer - $twriter";
	}
}

code should be

 

$query = "SELECT DISTINCT writer FROM data";
$result = mysql_query($query) or die ("Can't do anything with the query!");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
extract($row);
echo "$writer<br>";
$query1 = "SELECT count(quality) AS twriter1 FROM data WHERE writer='$writer' AND date BETWEEN '$year-$month-00' AND 			'$year-$month-32' ";
$result1 = mysql_query($query1) or die(mysql_error());
while ($row1 = mysql_fetch_array($result,MYSQL_ASSOC))
{
	extract($row1);
	echo "$writer - $twriter";
}
}

or do the same thing with

 

$query = "SELECT count(*) as twriter, writer FROM data group by twriter";
$result = mysql_query($query) or die ("Can't do anything with the query!");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
	extract($row);
	echo "$writer - $twriter";
}

Seems to me like you have a non-normalised database layout. You have duplicate data there that should not be.

Secondly do NOT do a query inside a query while still using mysql_... functions. They overwrite each other...

$sql1 = "select * from ...";
$res1 = mysql_query($sql1,$conn);
while($row1 = mysql_fetch_assoc($res1){
  
   $sql2 = "select * from ...";
   $res2 = mysql_query($sql1,$conn);
   while($row2 = mysql_fetch_assoc($res2){
        /// BLAM!!!!
        /// WE JUST OVERWROTE OUR OUTER LOOP...
   }
}

 

So you're probably wondering what you should do about this. Use arrays...

 

$sql1 = "select * from ...";
$res1 = mysql_query($sql1,$conn);
$data1 = array();
while($row1 = mysql_fetch_assoc($res1){
   $data1[] = $row1;
}

$sql2 = "select * from ...";
$res2 = mysql_query($sql2,$conn);
while($row2 = mysql_fetch_assoc($res2){
   // Use data1 array or do something else.  
}

 

If you don't understand why the first loop is being messed then you best read up on mysql_fetch... ;)

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.