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
https://forums.phpfreaks.com/topic/229387-pdo-to-determine-if-values-are-unique/
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.

 

 

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.