Solved

php mysql help, is it already in db?

Posted on 2009-07-05
4
188 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

26 Experts available now in Live!

Get 1:1 Help Now