Solved

php mysql help, is it already in db?

Posted on 2009-07-05
4
189 Views
Last Modified: 2012-05-07
I need to see if an item is already in the database, I can do this:

--------------------------
$query = "SELECT * FROM oys_reg_users WHERE reg_email = '".$email."'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
if ($num_results == 0) {
--------------------------

but it seems like there would be a more efficient way.... do I use count rows, or something else?  I just need to know if it already exists or not,

Thanks,  Chris
0
Comment
Question by:St_Aug_Beach_Bum
4 Comments
 
LVL 2

Accepted Solution

by:
Erbureth earned 500 total points
ID: 24780097
If you only want to verify whether the result exists, you could use
SELECT COUNT(*) FROM table WHERE oys_reg_users WHERE reg_email = '".$email."'";

which is a little bit faster, as it returns only the result count, instead of whole set of results.
0
 
LVL 5

Expert Comment

by:rizwanidrees
ID: 24780105
You should use COUNT(*) to make it little bit faster, however it is also fine
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 31599918
Thank you both!  Appreciate the help :)
0
 
LVL 27

Expert Comment

by:yodercm
ID: 24780217
If the reason you need to know whether it exists or not is so you can decide whether to INSERT or UPDATE, consider MySQL's REPLACE.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now