Jump to content

problem with select distinct


rpjd

Recommended Posts

I have 5 entries in a table

$sql = "select count(distinct columnName) from table";
$result = mysql_result($sql);
$count = mysql_fetch_array($result);
echo $count[0];

The output is 5 as expected.

$sql = "select distinct columnName from table";
$result = mysql_result($sql);
$count = mysql_fetch_array($result);
echo $count[0];

the ouput is the first file name as expected, however

echo $count[1];

gives undefined offset 1, which does not make any sense.

Can anyone explain why the offset 1 is undefined if the count is 5?

 

 

Link to comment
Share on other sites

Nearly there if think.  I want to put the changes to each file into a seperate table, a table for each file.

I have this:

$AmendmentsQuery = "select File, Text from Changes  group by File order by File";
$Amendments= mysql_query($AmendmentsQuery);
$FilesQuery = "select distinct File from Changes order by File ASC";
$Files= mysql_query($FilesQuery);
$row = array();
while($rows = mysql_fetch_array($Files)) 
	{
	$row[] = $rows;
	while($changes = mysql_fetch_array($Amendments))
		{
		for($i=0; $i<count($row); $i++)
			{
			if($rows['File'] = $row[$i])
				{
				echo $changes['Reference'] . "</td><td>" . $changes['Text'];
				}
			}
		}
	}

Instead of printing the file name it prints array, it prints the text .  If I use this

while($refs = mysql_fetch_array($Reference)) 
	{
	$row[] = $rows;
	while($changes = mysql_fetch_array($Amendments))
		{
		echo $changes['Reference'] . "</td><td>" . $changes['Text'];
		}
	}

It prints both filename and text.  Can anyone explain why the code above won't print the file name? 

Link to comment
Share on other sites

I got it printing both the file name and text, but its all in the one table.  I want to compare the file name in the first (outer) while loop to the file name in the second (inner) while loop and create a new table when the file name changes, but not having any luck figuring out how. 

while($row = mysql_fetch_array($Files))

{

echo "<table border='1'>";

while($changes = mysql_fetch_array($Amendments))

      {

      echo "<tr><td>" . $changes['Reference'] . "</td><td>" . $changes['Text'];

      }

echo "</table>";

}

 

Link to comment
Share on other sites

Trying to implement mysql_data_seek() but keep getting an infinite loop.  Not entirely sure I'm implementing it correctly mind u. 

$row = array();

while($rows = mysql_fetch_array($Files))

{

$row[] = $rows;

                for($x=0; $x<mysql_num_rows($Amendments); $x++)

                    {

                    if(mysql_data_seek($Amendments, $x))

                      {

        while($changes = mysql_fetch_array($Amendments))

      {

      for($i=0; $i<count($row); $i++)

  {

    if($rows['File'] = $row[$i])

{

echo $changes['Reference'] . "</td><td>" . $changes['Text'];

}

  }

      }

        }

                    }

              }

Link to comment
Share on other sites

Try this code:

 

while($rows = mysql_fetch_array($Files)) {
  $current_file = $rows['File'];
  print "<table><tr><td>";
  mysql_data_seek($Amendments, 0);
  while ($changes = mysql_fetch_array($Amendments)) {
    if ($changes['File'] == $current_file) {
      echo $changes['Reference'] . "</td><td>" . $changes['Text'];
    }
  }
  print "</td></tr></table>";
}

 

Make sure you are selecting Reference from the table as well if you are going to try to display it.

Link to comment
Share on other sites

I don't know if you have tested it or not, but all the files have their own table except for table1.  Even though the data seek is set to 0 and I start the table immediately after the first while.  I put a border on the table just to make sure.

while($rows = mysql_fetch_array($Files)) 
      {
      print "<table border='1'>";
      $current_file = $rows['File'];
      mysql_data_seek($Amendments, 0);
      while ($changes = mysql_fetch_array($Amendments))
             {
             if ($changes['File'] == $current_file) 
               {
               echo "<tr><td>$changes['File'] . "</td><td>" . $changes['Text'] . "</td></tr>";
               }
             }
      print "</table>";
      }

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.