Can I use a SELECT within the IF?

-2

It occurred to me that I tried to do this, but it seems to be wrong. Is it possible?

//EVITANDO DUPLICIDADE
if ($legenda > "0") {
    SELECT legenda FROM aula_upload_arquivo WHERE legenda = $legenda;
    echo 'Existe esse arquivo';
} else {
    echo 'Não existe o arquivo';
}
    
asked by anonymous 04.12.2018 / 21:29

3 answers

2

You can, but not this way. To use SQL you should use some function for this, such as mysqli_query or equivalent.

That way, it would be like doing:

if ($legenda > 0) {
   $result = mysqli_query($con, "SELECT legenda FROM aula_upload_arquivo WHERE legenda = '$legenda'");
   // ...
} else {
   // ...
}

There is nothing wrong with running a query within a if . If that's what you're doing, then mention the exact error that occurs.

    
04.12.2018 / 21:47
0
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$result = mysqli_query($conn, "SELECT * FROM table WHERE dado='$dado'");
if(mysqli_num_rows($result) > 0){
echo "Este Dado já existe";
exit();
} else {
 if(mysqli_query($conn, "INSERT INTO table (dado) VALUES ('$dado')");
 echo "Dados inseridos com sucesso";
}
    
04.12.2018 / 21:51
0

PROBLEM SOLVED:

01) It was not necessary to do this with SELECT within the IF. 02) Much easier, was to transform one of the fields into UNIQUE 03) In order for the data to always be unique, the information was entered in the field, using the user name and its ID, along with any noun.

    
05.12.2018 / 05:26