Jump to content


Redirect if record already exists in database

  • Please log in to reply
2 replies to this topic

#1 ramone_johnny

  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationSydney, NSW

Posted 25 May 2013 - 03:25 AM

I have the following snippet of code that I *think* is right, but being new to PHP I just wanted if someone could let me know if this is indeed correct.


I'm simply redirecting the user if they are already a member (email exists in the db)

$r=query_wrapper("SELECT * FROM tblmembers WHERE mem_email = ?",$mem_email);


Works Media - Online Marketing & SEO

#2 Strider64

  • Members
  • PipPipPip
  • Advanced Member
  • 247 posts
  • LocationA burb of Detroit, MI

Posted 25 May 2013 - 04:04 AM

if ($stmt = mysqli->prepare("SELECT * FROM tblmembers WHERE mem_email=? LIMIT 1")) {
    $stmt->bind_param("s", $mem_email);
// If count is greater than 0 then you know email exists:
if ($count) {

 http://www.pepster.com  ||  "Insanity: doing the same thing over and over again and expecting different results." - Albert Einstein 

#3 mac_gyver

  • Staff Alumni
  • Staff Alumni
  • 4,048 posts

Posted 25 May 2013 - 02:28 PM

there's nothing functionally wrong with your code because mysql_fetch_assoc($r); will return a false value if the query did not match any row (or if the query failed due to an error). this also assumes that your redirect() function executes an exit/die statement after the header() redirect to prevent your code from continuing to run.


however, if you are not going to use the actual data from the query and just need to find out if there's a matching row(s) with some value, it is more desirable to use COUNT() in a query, then get and test that count value -

$r=query_wrapper("SELECT COUNT(*) FROM tblmembers WHERE mem_email = ?",$mem_email);

if($count > 0)

Edited by mac_gyver, 25 May 2013 - 02:31 PM.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users