Jump to content

problem searching for email address in table


phill2000star

Recommended Posts

Hiya all,

I am writing a script at the moment that basically checks an email address isn't already stored in a MySQL DB before adding it. I am getting stuck though and can't see why. Can anyone help me??

Here's the code.

=================================
[code]$userExists = false;

// open DB connection
include '../db/config.php';
include '../db/dbOpen.php';
$sql = "SELECT id, email FROM tbl_mailing_list WHERE email=$email";

$result = mysql_query($sql);

if (!$result) {
  echo "Could not successfully run query ($sql) from DB: " . mysql_error();
  exit;
}

if (mysql_num_rows($result) == 0) {
  echo "No rows found, nothing to print so am exiting";
  exit;
}[/code]

=================================

Now when I submit an email address e.g. [email protected] to the script and an email address already exists, I get the following error.

[b]Could not successfully run query (SELECT id, email FROM tbl_mailing_list WHERE [email protected]) from DB: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@somedomain.net' at line 1[/b]

Now if I was to remove the section of SQL "WHERE email=$email" and replace it with "WHERE email='[email protected]'" then it works perfectly!!

Im pulling my last strand of hair out as I type!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.