2013-08-27 44 views
0

我試圖使用的基本控制結構是使用用戶名和電子郵件查詢數據庫,兩者都是唯一鍵,並且如果其中一個在數據庫中讓用戶知道他們已被採取並請其他人採取其他措施。我遇到的問題是以可用的形式獲取結果數據,然後我可以檢查用戶提供的數據。無法處理來自MySQLI的SELECT查詢的結果

我從snippit以及驗證例程中刪除了準備插入的語句,因爲它們都工作正常。

DB connection snippit

try { 
    if(!($dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME))){  // Creates the $dbc variable object so we can 
                      // have a connection to the database. 
                      // uses mysqli functions. 
    throw new Exception; 
    } 

} 
catch (Exception $e) { 
    echo '<p>Could not connect to the database. Please contact the system administrator.</p>'; 
} 

註冊腳本的SNIPPIT

//before this was validation routines, if anything was wrong the script generated something into $reg_errors which is an array. 
if(empty($reg_errors)) 
{ 
//queries database if there are any matches for username or email from user input. 
if($stmt = $dbc->prepare("SELECT `email`, `username` FROM `users` WHERE `email` = ? OR `username` = ?")) 
{ 
    $stmt->bind_param("ss", $e, $u); 
    $stmt->execute(); 
    $stmt->store_result(); 
    $rows = $stmt->num_rows;     //gives the number of rows returned from SELECT query. 0 means no dupes, 1 means one record has BOTH email and username, 2 means two different records (one with email, one with username) 

##THIS IS WHERE I'M RUNNING INTO TROUBLE GETTING THE DATA IN A USABLE FORM## 

    $stmt->close(); 
} else { 
    echo "<p>Can't talk to database right now. Try again later, please.</p>"; 
} 

     if($rows==0)   //no dupes of username or email, so let's try and add them into the DB 
     { 
      //prepared statement for insertion into DB 
      //also get's the count of affected rows. 1 means record inserted correctly. 

     //asks DB if a new row was created, and if so, thanks user for 
     //registration on the site & sends an email to their email. 
     //if query doesnt work, an error is triggered 
     if($count==1) { 
      //constructs a thank you note and emails it to the user, using the email they supplied. 
      exit(); 
      } else { 
       echo "<p>Unable to process your registration at this time. Please try again later..</p>"; 
      } 
    } else {     // both username and email might be already used in DB, and error msgs are generated for array. 
     if($rows==2) {   // this checks to make sure both entries are dupes   
      $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.'; 
      $reg_errors['username'] = 'This username has already been registered. Please try another.'; 
     } else {    //this checks to see which of the two (email or username) is already in DB if both arent dupes. 

      if((__NEED SOMETHING HERE FROM DB QUERY___ == $_POST['email']) && (__NEED SOMETHING HERE FROM DB QUERY___ == $_POST['username'])) { //both match entries in DB 
       $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.'; 
       $reg_errors['username'] = 'This username has already been registered with this email address. If you have forgotten your password, use the link to the right to have your password sent to you.'; 
      } elseif(__NEED SOMETHING HERE FROM DB QUERY___==$_POST['email']) {  // email match 
        $reg_errors['email'] = 'This email address has already been registered. If you have forgotten your password, use the link to the right to have your password sent to you.'; 
      } elseif(__NEED SOMETHING HERE FROM DB QUERY___==$_POST['username']) {  // username match 
       $reg_errors['username'] = 'This username has already been registered. Please try another one.'; 
      } 
     }  // end of $rows==2 ELSE 
    }   // end of $rows == 0 IF 

    } else {   // end of empty reg_errors conditional 
     //do something if the reg_error array isnt empty.. 
    } 

我敢肯定,答案就在迭代,並使用從結果的mysqli對象meta_data,但打我的頭靠在後一個牆壁連續幾天,像瘋子一樣在mysqli php手冊頁上流淌,我仍然沒有仔細研究我應該做什麼。任何人都可以指出我正確的方向嗎?

+0

你知道嗎,你的使用異常的想法很嗯...原來。 –

+0

我試着讀你的代碼,但放棄了。太多的滾動我的可憐的小老鼠。 –

+1

說到你的問題 - 這只是你的急躁。在獲得任何註冊碼之前,您必須練習簡單的mysqli示例,讓自己熟悉基本操作。 –

回答

0

從註冊腳本開始,你有沒有試過這樣:

if($stmt = $dbc->prepare("SELECT `email`, `username` FROM `users` WHERE `email` = ? OR `username` = ?")) 
{ 
    $stmt->bind_param("ss", $e, $u); 
    $stmt->execute(); 
    $stmt->bind_result($email, $username); 
    $rows = $stmt->num_rows; 

    //Move Conditionals Up a Little 
    if($rows == 0) { //If No Records are Found 

     //Continue Registration 
    } 
    else if($rows == 1) { //If One Record is Found 

     $stmt->fetch(); 

     //Do Something With $email and $username from DB Here 

    } 
    else { //If More than One Record is Found 

     while($stmt->fetch()) { //Iterate Through Records 

      //Do Something With $email and $username from DB Here 
     } 
    } 
} 
+0

你的邏輯,通過結果集是驚人的。謝謝,這有些幫助。如果我有代表,我會贊成這一點。這不是完整的答案,但絕對不止是朝着正確方向邁出的一步。謝謝。 – cst003

+0

在這種情況下,我的答案停在哪裏? – Jason