?
Solved

php mysql help, is it already in db?

Posted on 2009-07-05
4
Medium Priority
?
195 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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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