Jump to content

PDO To Determine if values are unique


guyfromfl

Recommended Posts

I have a Unique Constraint setup on (emailAddress and pin) to allow multiple email addresses be the same and multiple pins but not the same emailAddress and pin value together (unique(emailAddress, pin))

 

Anyway, I am trying to generate a pin through a random seed, but I can't get my code to work properly to test if the combination already exists.

 

Here is the chunk that does not work:

 


$tries = 0;
	$foundRows=0;
	do {
		$pin=null;
		$tries++;
		$pin = str_pad(rand(0,9), 4, "0", STR_PAD_LEFT);


		$stmt = $dbh->prepare("SELECT COUNT(*) FROM ajax WHERE emailAddress=:email AND pin=:pin LIMIT 1");
		if ($stmt->bindParam(':email', $email, PDO::PARAM_STR) &&
			$stmt->bindParam(':pin', $pin, PDO::PARAM_INT)) {
				$stmt->execute();
				$foundRows = $dbh->query("SELECT FOUND_ROWS()")->fetchColumn();
				echo "<br />$foundRows<br />";
		} else {
			die ("<p>Could not bind to data verification</p>");
		}
	} while ($foundRows > 0);

 

This is always returning $foundRows=0 so always exiting the loop, and giving me a constraint error.

 

Any ideas? Maybe a better way to do this?

Link to comment
Share on other sites

I am setting $pin=null to reset it.

 

I was going to have another testing condition, but took that part out and left the initializaiton in, wasn't really paying attention to that i guess.

 

I think the problem is I added that do loop before I had prepared all the variables, leaving $email null, and it was tripping it up.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.