Page 1 of 1

php fetch from mysql limit same ID's

Posted: Wed Mar 17, 2010 3:22 pm
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

Re: php fetch from mysql limit same ID's

Posted: Wed Mar 17, 2010 5:16 pm
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.

Re: php fetch from mysql limit same ID's

Posted: Thu Mar 18, 2010 8:07 am
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 :)