views:

137

answers:

2

This is a really broad question, but I have come across it a couple of times in the last few weeks and I was wondering what the general consensus is regarding good practice and efficiency.

1)

SELECT COUNT(*) FROM table WHERE id='$id', name='$name', owner='$owner_id'

and then based on if there is one result then the record matches.

2)

SELECT * FROM table WHERE id='$id'

and then a series of if commands to check the results match.

Now obviously there are advantages to the second solution as it allows for accurate error reports as to the field that does not match... but if that is not required which is more efficient, considered better practice and is there a difference to the load on the mySQL server between the two?

+1  A: 

Option 1 by a long shot. Let SQL do what it is designed to do best, and better than procedural code. That is, filtering and sorting data.

Also, it is a much more efficient use of resources (bandwidth, DB utilization, etc) to pull down only the data you need from the server.

JohnFx
+1  A: 

Use 1). Mysql is very efficient in selecting data based on certain conditions.

Joel L