2
Good staff my question is the following: How can I be doing the check to know if there is already such value in the database?
I have this code here that is already checked by SQL Query itself, ie if there is no such value it inserted and if there is no it does not insert, This is working perfectly.
$sql = 'INSERT INTO usuarios(nome,apelido)
SELECT "'.$valor[0].'", "'.$valor[1].'"
FROM DUAL
WHERE NOT EXISTS(SELECT nome,apelido FROM usuarios
WHERE nome = "'.$valor[0].'" OR apelido = "'.$valor[1].'")';
$resultado = $MySQLi->query($sql) OR trigger_error($MySQLi->error, E_USER_ERROR);
if($resultado == 1) {
echo 'sucesso';
} else {
echo 'já existe esse nome ou nick';
}
The problem is to receive this notification and tell the user if it already exists, because it always returns 1 in the $result variable so I can’t tell the user if it was entered or if the value already exists.
Could someone tell me a functional way to make that check?
I don’t know which API you use to do the queries but the idea is to take the number of rows returned by the query,
$resultado
It seems to me a Source, then your command isse(resource == 1)
, when it should be something likese($total_registros >= 1)
– rray
Fine, I’ll post here the Class I’m using to make the query.
– Viniam
http://blog.thiagobelem.net/php-e-mysql-para-iniciantes-consulta-simples
– Viniam
Can you tell if it’s Mysql or Mysqli? I don’t have access to the link ... try to get the number of lines with mysqli_num_rows, must look something like this,
$resultado = $MySQLi->query(....); echo $resultado->num_rows;
should appear some number, then just make the right comparison ;)– rray
if($mysqli->affected_rows >= 1){ echo 'sucesso';} else {echo 'já existe'; };
– user28595
All right, I’ll test it and I’ll get back to you.
– Viniam
Diego Felipe, Thank you very much the Code Techo you sent worked perfectly.
– Viniam