PDA

View Full Version : MySQL - There must be a better way . . .


B&T
3-9-04, 08:34 AM
I have two situations where I know there must be a simple/better way to do what I am doing.

1) I simply want to see if a value is in the database. I am currently doing a select using where for that elements value. Then I load the array and check to see if anything is there. There must be a simple one-step way to do this?

2) I have many rows (say 100) with duplicated values in an element (so say there are 10 unique element values and 90 are duplicates). I want to get the 10 unique values. I am doing a select * to read everything. Then in my script I run through the array and find unique values for the element. There must be a simple way to have the sql return only 10 rows (in my example) rather than have that extra code in my script?

Just trying to clean up some code.

Pig
3-9-04, 09:09 AM
#1 You can shave off a step by using mysql_num_rows.
$result = mysql_query($sql);
if( mysql_num_rows($result) > 0 ) { do_stuff(); }

#2 Check out DISTINCT in the manual.
SELECT DISTINCT(unique_id) FROM table

B&T
3-9-04, 09:37 AM
Thanks Pig :)

Pig
3-9-04, 10:35 AM
My sole purpose in life is to bring pleasure to my companions.