rasta876 Posted October 27, 2008 Share Posted October 27, 2008 Hello all, I used the code below to get the distinct names from my table How do I get the individual rows after the query (for example the first or second one, etc..). Id like to echo the rows individually as I like. The following code echoes all the names from the query $query="SELECT DISTINCT name FROM myTABLE"; $results=mysql_query($query); while($row=mysql_fetch_assoc($resluts)){ echo $row['name']"<br>"; } this shows for example: John Susan Kerry What do I do if Id just like to echo one name from the query? example: Susan Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/ Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 $name = 'Susan'; $query="SELECT * FROM myTABLE WHERE name='{$name}'"; $results=mysql_query($query); while($row=mysql_fetch_assoc($resluts)){ print_r($row); } Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-675579 Share on other sites More sharing options...
revraz Posted October 27, 2008 Share Posted October 27, 2008 $query="SELECT DISTINCT name FROM myTABLE WHERE name = 'Susan'"; Or, in your while loop while($row=mysql_fetch_assoc($resluts)){ if ($row['name'] == "Susan") { echo $row['name']"<br>}"; } Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-675582 Share on other sites More sharing options...
rasta876 Posted October 27, 2008 Author Share Posted October 27, 2008 Thank you for the quick reply, however im looking for a different result. In my Table I have many rows with the same name (e.g.Susan many times), I used DISTINCT in my query to get rid of the repitition. Is it possible to echo one name from the results. For example referencing them by their position or something. I cant set $name = 'Susan' because this value will occasionally change in the table when updated. The names will be replaced by new ones. Id like to just get one. Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-675597 Share on other sites More sharing options...
revraz Posted October 27, 2008 Share Posted October 27, 2008 And how do you determine which one you want to get? If you have many rows that have the same "name", then use an ID instead. Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-675612 Share on other sites More sharing options...
rasta876 Posted October 27, 2008 Author Share Posted October 27, 2008 If I can filter say 50 names(including the same name more than once) to just 5 names (no repitition) using DISTINCT and echo all five of them echo $row['name'] , how do I do the same thing but this time echo only one of the five without knowing the actual value, (something like echo $row + something unique for the each item)? I can do this echo $row['name']; to get John Susan Kerry Tim Mika I want to do something like this: echo $row[?????]; to get the second item which in this case would be: Susan and then when my table content changes, the code echo $row[????]; would still be referring to the second item, which would be a different name this time around. I hope you understand what I want Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-675640 Share on other sites More sharing options...
rasta876 Posted October 28, 2008 Author Share Posted October 28, 2008 Finally, I tried using mysql_data_seek and it worked. Thank you all for your effort. $query="SELECT DISTINCT name FROM myTABLE"; $results=mysql_query($query); mysql_data_seek($results,2 ) //to get the second item. while($row=mysql_fetch_assoc($resluts)){ echo $row['name']"<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/130274-solved-individual-row-from-query/#findComment-676303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.