Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php mysql help, is it already in db?

Posted on 2009-07-05
4
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

by:
Erbureth earned 2000 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:Cornelia Yoder
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

610 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