2013-06-08 12 views
7

Ho il seguente codice per un modulo di iscrizione. Voglio controllare l'e-mail per vedere se è già registrato nel database e in tal caso restituire un errore.Controllare se mysqli_query non ha valori?

$name = $_POST['name']; 
$email = $_POST['email']; 

$result = mysqli_query($connection,"SELECT * FROM users WHERE email='$email'"); 

if($result=="") 
{ 
echo("email was not found"); 
} 
else 
{ 
    echo("email was found"); 
} 

Ho problemi se i risultati tornano vuoti. Ho provato a utilizzare diverse cose e non riesco a capire come farlo funzionare correttamente se non viene trovato nulla.

risposta

6

Utilizzare mysqli_num_rows per verificare se le righe sono state restituite o meno.

+0

Sapevo che mi mancava qualcosa di semplice come questo! Grazie! –

1

Utilizzare questo.

$name = $_POST['name']; 
$email = $_POST['email']; 

$result = mysqli_query($connection,"SELECT * FROM users WHERE email='".$email."'"); 

if(!$result) 
{ 
    echo("email was not found"); 
} 
else 
{ 
    echo("email was found"); 
} 
+2

non ha funzionato per me, mysqli_num_rows ($ result) == 0 ha funzionato. –

-1
if(empty($result)) 
{ 
    echo("email was not found"); 
} 
+0

non ha funzionato per me, mysqli_num_rows ($ result) == 0 ha funzionato. –

1

uso mysqli_num_rows come questo

if(mysqli_num_rows($result)==0) echo("email was not found"); else echo("email was found"); 

spero che vi aiuterà u ..

Problemi correlati