If with database select variable

Asked

Viewed 51 times

-3

Good morning, everyone!

I am starting and with a basic doubt... I need to perform an if with a database information. I connect and select it, then I need the variable to use in if.

$idusuario  = $_SESSION['idusuario'];
$categorias = "SELECT * FROM categorias WHERE idusuario = '$idusuario'";
$resultado  = mysqli_query($con, $categorias);

Here, I need to compare the value of the "confirmed" column of the database to the user who is logged in, something like:

if $confirmado == 0

{

echo "XXXXX";

}

Can you help me with that question?

Thank you!

  • 1

    here on the site is full of examples such as: https://answall.com/q/209975/57220 and https://answall.com/q/133463/57220

  • These examples are using while, examples like this really have a lot. But this is the only way? Because I thought there was a simpler one!

  • 1

    Just exchange the while for if. The difference is that while goes through all the lines and if it will only get the first one (if the query only returns one line, it will not make a difference)

  • a select always returns a list of results, you need to fetch, even if only to get the first result

1 answer

2


If the ratio is 1 to 1 in the category table, you can write it like this:

$confirmado = $mysqli->query("SELECT confirmado FROM categorias WHERE idusuario = '$idusuario'")->fetch_object()->confirmado ;
if($confirmado == "<valor desejado>") {
// seu código aqui
}

Remember that this example only serves if your return corresponds to 1 result only, otherwise you should follow the example of the comment of our colleague Ricardo Punctual, and solve using while.

Select Sum in table with PHP/Mysql

  • That’s what I need and what I meant, it’s only 1 record, that of the user who is logged in! Thank you. I’ll take the test!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.