运行if else else if if语句

时间:2015-08-19 19:10:08

标签: php if-statement mysqli

我的代码首先执行“else if”语句,因为“if”语句无效。但是在完成执行“else if”语句之后,它会进入if语句,该语句现在有效。如何让它每回合只执行一个语句?我的意思是“if”和“elseif”语句的要点是最初选择有效的语句,并在执行该语句后停止运行代码。

<?php
//mysql_query("SET NAMES utf8"); 
$con=mysqli_connect("mysql_host","mysql_user","mysql_password","mysql_database");

$email = $_POST["email"];

$query = mysqli_query($con, "SELECT * FROM table WHERE email='{$email}'");

if(mysqli_num_rows($query) >0){

    $totalt2 = array();

    $totalt2[one] = 'already_exists';
    $totalt2[two] = 'already_exists';

    $encodedArray2 = array_map(utf8_encode, $totalt2);
    echo json_encode($encodedArray2);
    mysqli_close($con);
} else if(mysqli_num_rows($query) <1){

    $statement = mysqli_prepare($con, "INSERT INTO table (email) VALUES (?) ");
    mysqli_stmt_bind_param($statement, "s", $email);
    mysqli_stmt_execute($statement);

    mysqli_stmt_close($statement);

    $totalt = array();

    $totalt[one] = 'newly_created';
    $totalt[two] = 'newly_created';
    $encodedArray = array_map(utf8_encode, $totalt);
    echo json_encode($encodedArray);
    mysqli_close($con);
}
?>

0 个答案:

没有答案