Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...