Error adding two WHERE conditions in a MySQLi consula?

2

I want to show data using two conditions WHERE , questions by categories and that these questions are enabled to the public.

I do the following:

$categoria = "categoria_uno";
$habilitado = "1";
$stmt = $conexion->prepare("SELECT id,pregunta,fecha FROM questions WHERE categoria=? AND WHERE habilitado=? order by id DESC");
$stmt->bind_param("si",$categoria,$habilitado)

But it shows me an error in the line: $stmt->bind_param("si",$categoria,$habilitado)

Showing me the following:

  

Fatal error: Call to a member function bind_param() on boolean

Because the error, that another parameter must pass in WHERE categoria=? AND WHERE habilitado=?

    
asked by Zans 04.08.2018 в 18:26
source

2 answers

2

It is not necessary to add more than once WHERE , it is not necessary to repeat it for each condition that you want to add to the query.

You must remove the WHERE additional, as follows:

WHERE categoria=? AND habilitado=?

And so on, if you want to pass more conditions, simply add another AND example:

WHERE categoria=? AND habilitado=? AND otracondicion=? AND otros=?

At the end the query should be in such a way:

$stmt = $conexion->prepare("SELECT id,pregunta,fecha FROM questions WHERE categoria=? AND habilitado=? order by id DESC");
    
answered by 04.08.2018 / 18:42
source
1

You have 2 bad details:

  • If the field enabled in your bind_param () marks it as integer, then remove the quotes from the value in the variable
  • remove the where it is from more

    <?php $categoria = "categoria_uno"; $habilitado = 1; $stmt = $conexion->prepare("SELECT id,pregunta,fecha FROM questions WHERE categoria=? AND habilitado=? order by id DESC"); $stmt->bind_param("si",$categoria,$habilitado)

  •   

    You must understand that if you add quotation marks to a value, you are   transforming to text string

        
    answered by 04.08.2018 в 18:45