Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print every row only once 2

Status
Not open for further replies.

lukelukeluke

Technical User
Joined
Dec 23, 2003
Messages
117
Location
CH
Hi there!
I have a little script which looks like the following:
Code:
<?php
$kate = "'Webserver'";
$sql = 'SELECT * FROM knowledgebase_artikel WHERE kat1 LIKE '.$kate;
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) { echo "$row[kat2]<br>";
}
?>
This gives me the row "kat2" out. As you see i got a <br> there so it gives me a list of things from the mysql table vertically, lets say these:

Apple
Tree
Telephone
Apple
Computer
Tree

You can see that there are entrys which are the same. But i would like to show every entry only one time also if it is more than one time into the database. How can i do that?
Thanks!
 
Hi

Change your SQL to read:
$sql = 'SELECT DISTINCT * FROM knowledgebase_artikel WHERE kat1 LIKE '.$kate;

[cheers]
Cheers!
Laura
 
Solution 1:
Code:
$sql = "SELECT DISTINCT(kat2) FROM knowledgebase_artikel WHERE kat1 LIKE ".$kate;

Solution 2:
Code:
while ($row=mysql_fetch_assoc($result)) {
  $values[] = $row['kat2'];
}

$values = array_unique($values);

 
thank you very much for these workful solutions.
It worked very well for me!:

Code:
<?php
$sql = 'SELECT DISTINCT kat1 FROM knowledgebase_artikel WHERE 1 LIMIT 0, 30'; 
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) { echo "$row[kat1]<br>";
}
?>
 
Suggestions:
1. Are using $numrows anywhere? If not, don't waste the time to execute that statement.
2. Your query statement (mysql_query()) has no error catching. I recommend to append an OR die() clause.
Code:
$result = mysql_query($sql) OR die("MySQL error: ".mysql_error())
;
3. It is generally recommended to quote associative array keys.
Code:
echo $row['kat1']."<br>";
Remember that named constants would replace an unquoted array key if the name matches.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top