php - mysql phpmyadmin User Input COMPARE database -
hi guys i'm new php , mysql, trying check user input &username if there's existing same username in database, , same user input $email.
i have below codes, tried having line echoes , compares user input against fetch rows/ data database. able verify reaches point line echo $email."compare".$result2; both same, yet passes condition $email == $result2 result2 email fetch database. can please point me what's wrong? in advance.
$extract= mysql_query("select * users"); $resultq = mysql_num_rows($extract); while($row= mysql_fetch_array($extract)) { $result = $row['username']; $result2 = $row['email']; echo $email."compare".$result2; if($username == $result) { echo "<script type=\"text/javascript\">alert('username taken')</script>"; echo "<meta http-equiv=\"refresh\" content=\"0;url=6signup.html\" />"; break; } else if ($email == $result2) { echo "<script type=\"text/javascript\">alert('email registered')</script>"; echo "<meta http-equiv=\"refresh\" content=\"0;url=6signup.html\" />"; break; } else { //continues execute loop until no more fetch }
i use different approach , search in sql-query. sql better (and performs better) on kind of searches.
you won't need while loop query:
select * users username = '<input username here>' or email = '<input email here>'
if result, means either username or e-mailaddress registered. use if-statement check whether or not it's username or e-mailaddress.
please, don't use
mysql_*
functions in new code. no longer maintained and officially deprecated. see red box? learn prepared statements instead, , use pdo or mysqli - this article decide which. if choose pdo, here tutorial.
edit:
the full php-code (for pdo):
// database connection // ------------------- // put in seperate file , include it. try { $dbconn = new pdo('mysql:dbname=database;host=localhost', 'username', 'password'); } catch (pdoexception $e) { die('connection failed: ' . $e->getmessage()); } // prepare statement // ----------------- // build query using question mark parameters $sql = "select * users username = ? or email = ?"; // create prepared statement $sth = $dbconn->prepare($sql); // execute statement // ----------------- // parameters automatically changed input data $sth->execute(array($username, $email)); // put affected rows in $result (array!) $result = $sth->fetchall(); // check array contents // -------------------- if (count($result) > 0) { // loop through results foreach ($result $row) { if ($username == $row['username']) { echo "<script type=\"text/javascript\">alert('this username taken!')</script>"; echo "<meta http-equiv=\"refresh\" content=\"0;url=6signup.html\" />"; } else { // if username doesn't match, has e-mailaddress echo "<script type=\"text/javascript\">alert('this e-mailaddress has registered!')</script>"; echo "<meta http-equiv=\"refresh\" content=\"0;url=6signup.html\" />"; } } } else { /* code execute when not registered yet */ }
Comments
Post a Comment