2012-02-24 27 views
0

是否有人知道如何更正此代碼,以便識別電子郵件是否已存在於數據庫中並顯示錯誤。這是目前顯示錯誤消息,即使該電子郵件不存在於數據庫中 - 因此形式沒有得到提交:正確識別數據庫中是否存在電子郵件的問題 - PHP

$email = $_POST['email']; 
//prepare and set the query and then execute it 
$stmt = $conn2->prepare("SELECT COUNT(email) FROM users WHERE email = ?"); 
$stmt->bind_param('s', $email); 
$stmt->execute(); 

// grab the result 
$stmt->store_result(); 

// get the count 
$numRows = $stmt->num_rows(); 

if($numRows) 
{ 
$errors = true; 
echo "<p class='red'>Email is already registered with us</p>"; 
} 
else 


//if we have no errors, do the SQL 
+0

這段代碼有什麼問題? – apis17 2012-02-24 19:14:23

+0

更多有用的相關信息:http://stackoverflow.com/questions/2700621/php-pdo-num-rows – 2012-02-24 19:16:57

+0

看到我的編輯 – 2012-02-24 19:19:30

回答

1

篩選隊友:

$stmt = $mysqli->prepare("SELECT COUNT(email) FROM users WHERE email = ?"); 
$stmt->bind_param('s',$email); 
$stmt->execute(); 
$stmt->bind_result($count); 
while($stmt->fetch()){} 

if(!empty($count)){ echo "Already Registered"; } 

使用:

// grab the result 
$stmt->store_result(); 

,如果你離開另一個連接之前打開時才使用。

使用:

// get the count 
$numRows = $stmt->num_rows(); 

是一個愚蠢的方法,當你只是計算ID反正。

+0

感謝Dan Kanze。有效 :) – user1227124 2012-02-24 20:18:18

1

$numRows = $stmt->num_rows();總是會返回一個,因爲你只選擇一個行(即使COUNT(電子郵件)的值爲0)

$row = $stmt->fetch() 
echo $row[0]; // result of COUNT(email) 
0

SELECT COUNT(*)將始終返回1行...

您需要可以使用SELECT *或調整代碼查詢結果返回來自SQL語句

相關問題