guyfromfl Posted March 2, 2011 Share Posted March 2, 2011 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 More sharing options...
cunoodle2 Posted March 2, 2011 Share Posted March 2, 2011 why do you set this.. $pin=null; Link to comment https://forums.phpfreaks.com/topic/229387-pdo-to-determine-if-values-are-unique/#findComment-1181944 Share on other sites More sharing options...
guyfromfl Posted March 2, 2011 Author Share Posted March 2, 2011 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 https://forums.phpfreaks.com/topic/229387-pdo-to-determine-if-values-are-unique/#findComment-1181949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.