php fetch from mysql limit same ID's

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

php fetch from mysql limit same ID's

Post by me666 »

Hi all, have a little problem. I have details of every user in a table 'profileu' and when they comment on a picture the script will create a table and put their comment and Id in it. I am looking now to send an email to everyone that has also commented on it so I have made a little code below to search for all the users that have commented and get their email from profileu using their Id from the comments table..

Code: Select all

<?php
$othuyt = mysql_query("SELECT * FROM $comstbl");
While($othujk = mysql_fetch_object($othuyt)){
$otherc = mysql_query("SELECT * FROM profileu where id = '$othujk->frmid'");
$othcom = mysql_fetch_object($otherc);
$othem = $othcom->email;
<code to send details in email to $othem>
}
?>
That’s all fine, but if 1person has put say 5 comments on there, they will get sent 5 emails. I need to limit it so it can fetch many results from the comment table, but only take 1 of each Id. Everyone's Id is different.
Any ideas will be appreciated.
Thanks
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: php fetch from mysql limit same ID's

Post by Sofw_Arch_Dev »

I'm not sure what the structure of your comstbl is, but have you investigated the "DISTINCT" SQL keyword? Instead of selecting *, select DISTINCT user_id, as well as whatever else you specifically need from that table.
me666
Forum Commoner
Posts: 87
Joined: Wed Oct 08, 2008 5:04 pm

Re: php fetch from mysql limit same ID's

Post by me666 »

excellent, does the job perfect, all i need is the user id in this query so it gets what i need it to... thank you very much :)
Post Reply