在查询上使用while循环

时间:2015-05-31 10:49:43

标签: php mysql sql if-statement

我有以下查询,但我的问题如下:

 $insert_email = "select * from customers";
$run_email = mysqli_query($con, $insert_email);

$find_email = mysqli_fetch_array($run_email);
$demail = $find_email['customer_email'];

echo $demail;

我的问题是,在当前状态下,它只返回此customers表中的第一项,我希望它返回该行中的所有customer_email,而不仅仅是第一项。

这是因为我后来想比较它

if($email!= $demail){

如果用户在注册期间输入的电子邮件在数据库中找到,那么我将通过引入else语句告诉用户该电子邮件已被使用。

更新

$insert_email = "select * from customers where customer_email='$email'";
$run_email = mysqli_query($con, $insert_email);
if(mysqli_num_rows($run_email)>0){

    $crs_id = $_GET['crs_id'];
    $_SESSION['userCoupon'] = $_POST['couponCodeRegisterAmount'];
    $_SESSION['userCouponName'] = $_POST['couponCodeRegister'];


        $_SESSION['customer_email']=$email; 

         $insert_c = "insert into customers (customer_fname,customer_lname,customer_email,customer_pass,coupon_code_register,coupon_code_register_amount) values ('$fname','$lname','$email','$pass','$couponCodeRegister','$couponCodeRegisterAmount')";

        $run_c = mysqli_query($con, $insert_c); 


        echo "<script>window.open('coursePage.php?crs_id=$crs_id#attend','_self')</script>";


        echo "<script>

         document.getElementById('registerError').innerHTML = 'Account has been created successfully, Thanks!'
         </script>";

}

else {
    echo "<script>window.open('coursePage.php?crs_id=$crs_id#attend','_self')</script>";

    echo "<script>
        document.getElementById('registerError').innerHTML = 'This email has already been taken. Please use another one.'
       </script>";
}

3 个答案:

答案 0 :(得分:1)

您可以尝试使用子查询,请为我的PHP编码找借口。

SQL:(示例 - http://sqlfiddle.com/#!9/19101/1

INSERT INTO customers (email)
SELECT a.email
FROM (SELECT 'not exists email address' AS email) AS a
WHERE NOT EXISTS (SELECT 1 
                  FROM customers cus
                  WHERE a.email = cus.email);

PHP:(您也可以使用 $affected_rows 检查是否插入了行)

$todo_email = "not exists email address";

// This is where you will define your insert clause.
$insertClause = "INSERT INTO customers (email)";

// Here you can form a row of record for insert.
// Note: for multiple record use a while loop and use "UNION"
$tmpTableClause = "SELECT %s AS email", 
$tmpTableClause = sprintf($tmpTableClause, mysql_real_escape_string($todo_email));

// This will form the main query logic
$query = "SELECT a.email
          FROM ("+ $tmpTableClause +") AS a
          WHERE NOT EXISTS (SELECT 1 
                            FROM customers cus
                            WHERE a.email = cus.email);";

// Now join and run the real query
$query = $insertClause + $query;

答案 1 :(得分:0)

while ($find_email = mysqli_fetch_array($run_email))
{
  if($email == $find_email['customer_email']){  }
}

或者更好地使用它:

$insert_email = "select * from customers where customer_email='".$email."'";
$run_email = mysqli_query($con, $insert_email);
if (mysqli_num_rows($run_email)>0){}
else {echo 'no customers found with this email!'; }

答案 2 :(得分:-1)

$insert_email = "select * from customers where email='email_entered'";
$run_email = mysqli_query($con, $insert_email);

if(mysqli_num_rows($run_email)==0){
    //do whatever you wants
}else{
    echo "Email Already Exists";
}