Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

php - consultation programming while and if nested

I'm a little confused, not because I do not work this program. mails compares a database with a variable.

  $correo = "media@gmaeil.com";

$compruebaCorreo=mysqli_query($link, "SELECT * FROM email WHERE email='$correo'");


while($imprimeCorreo=mysqli_fetch_array($compruebaCorreo))
{
    if($imprimeCorreo['email']==$correo)
    {
        echo "Ya recibió los tps por su suscripción al Boletín de Todopolicia.com";
    }
    if($imprimeCorreo['email'] != $correo)
    {
        echo "Registramos el correo";
    }
    echo $imprimeCorreo['email'];
}

The issue is that if equal, if it meets the first if and echo the while, but if it is not, do nothing, nothing prints, or even the second miss. Where is the fault?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to check whether the email is in your database, you can do it like this.

  • prepare your select statement

    • by using prepared statements your sql query will always be well formatted, no matter if there are 'weird' charakters in your parameters which might break the sql syntax otherwise
  • bind the parameter to the statement and execute it

  • store the result of the statement

    • this way we are able to use mysqli_stmt_num_rows($stmt) in the next step
  • use mysqli_stmt_num_rows($stmt) to check, if the resultset contains at least 1 row

    • if yes: your email is in the database
    • if no: it is not

Code:

/**
 * define your select-statement and your parameter(s)
 * let the database prepare the statement and bind the parameters
 */
$stmt = mysqli_prepare($link, 'SELECT * FROM email WHERE email = ?');
mysqli_stmt_bind_param($stmt, "s", $correo);
$correo = "media@gmaeil.com";

/**
 * execute the statement and storing the result
 */
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

/**
 * check the resultset and react accordingly
 */
if(mysqli_stmt_num_rows($stmt) > 0){
    echo "Ya recibió los tps por su suscripción al Boletín de Todopolicia.com";
}else{
    echo "Registramos el correo";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...