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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT DISTINCT ?? 2

Status
Not open for further replies.

curvv

IS-IT--Management
Jan 11, 2001
103
ZA
Hi,

I used SELECT DISTINCT on an access db through odbc the other day and it worked fine. I'm trying to do it on a mySQL db, but nothing seem to work.

my query:
$q_end_usr = mysql_query("SELECT DISTINCT user_id FROM timerec.tbl_time ORDER BY user_id");

how do i get that value back through php?
i used all kinds of while structures, but nothing seems to work.

thanx ######## CtN ########
 
Try

//no need in this statement to include the
//table name (timerec.tbl_time)
//just use tbl_time
$q_end_usr = mysql_query("SELECT DISTINCT user_id FROM tbl_time ORDER BY user_id");

$r_end_usr = mysql_query($q_end_usr);

while($c_end_usr = mysql_fetch_row($r_end_usr)) {
echo $c_end_usr[0];
}

if this does not work, then test your records:

$q = mysql_query("SELECT COUNT(DISTINCT user_id) FROM tbl_time ORDER BY user_id");

$r = mysql_query($q);
$c = mysql_fetch_row($r);

echo $c[0];

Chad. ICQ: 54380631
 
thanx alot inland,

everything worx cool now. just for other people reading this.

$q_end_usr = mysql_query("SELECT DISTINCT user_id FROM tbl_time ORDER BY user_id");

$r_end_usr = mysql_query($q_end_usr);

should be:

$q_end_usr = ("SELECT DISTINCT user_id FROM tbl_time ORDER BY user_id");

$r_end_usr = mysql_query($q_end_usr);

just a typo

thanx again


######## CtN ########
 
Yes, curvv. Sorry about that. I was typing too fast!

And instead of
$q_end_usr = ("SELECT DISTINCT user_id FROM tbl_time ORDER BY user_id");

make sure to get rid of the ( and ) as well so you don't get a parse error.

Good catch.

Chad. ICQ: 54380631
 
Here's an easier way .

$q_end_usr = "SELECT DISTINCT(user_id) AS distinct_user FROM tbl_time BY user_id";

$r_end_usr = mysql_query($q_end_usr);

while ($record = mysql_fetch_array($r_end_usr)){
$distinct_user = $record[distinct_user];

echo &quot;$distinct_user <BR>&quot;;
};


cheers
devnull22

--
Apparently if you play the Windows NT CD backwards you hear satanic messages. If you think that's bad, play it forwards and it installs Windows NT !
 
devnull22,

Same number of steps and also no need to name the result column since it is the ONLY column being passed. Remember, mysql_fetch_array has a tendency to take just a little longer than mysql_fetch_row (maybe or maybe not noticeable) and mysql_fetch_row in this particular case is only needed (only one column being returned).

In any case, they both work. ;-)

Chad. ICQ: 54380631
 
true,

but much easier to debug later :) unless u don't plan to ever come back to the code.

cheers
devnull22

--
Apparently if you play the Windows NT CD backwards you hear satanic messages. If you think that's bad, play it forwards and it installs Windows NT !
 
Hi,

At least you can see I read and analyse the code, and not only copy and paste it.

Anyway, I tried both ways and they work fine.

thanx alot ######## CtN ########
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top