Warning: mysqli_stmt :: bind_param (): Number of variables does not match number of parameters

2

Hello everyone I am using mysqli and I have the following error:

  

Warning: mysqli_stmt :: bind_param (): Number of variables does not match   number of parameters in prepared statement on line 8

Please, if you can help me solve it, thank you:)

<?php

$conexion = new mysqli('localhost','','','');

if (mysqli_connect_error()) { echo mysqli_connect_error(); exit; }
    $q = "%" . $_POST['q'] . "%";
    $consulta= $conexion->prepare("SELECT ar.Titulo_Articulo, ar.Id_Articulo, r.Id_Revista FROM articulos as ar INNER JOIN revista as r on r.Id_Revista = 

ar.Id_Revista where ar.pdf LIKE ?");
    $consulta->bind_param("s", $q);
    $consulta->execute();
    $res = $consulta->get_result();
    if($conexion->affected_rows>0)
    {
      while($fila=$res->fetch_array())
        {

            echo '<a href="autenticacion.php?id='.$fila["Id_Revista"].'&articulo='.$fila["Id_Articulo"].'" width="50%" class="sugerencias" onclick="myFunction2('.utf8_encode($fila["Titulo_Articulo"]).')"><br>'.utf8_encode($fila['Titulo_Articulo']).'</a>';

        }
    }
    else
    {
      echo '<b>No hay sugerencias</b>';
    }
    $consulta->close();
    $conexion->close();
?>
    
asked by Quiroz Luis 21.04.2016 в 19:41
source

1 answer

1

The problem you have is the quotes in like : LIKE '%?%'") . You get that error because you put single quotes, the ? is then interpreted as a literal string and not as the marker of the parameter you want to bind.

Also, those quotes are unnecessary because in the bind_param you are already indicating that it will be a string (with the "s" of the first parameter). The solution would be simple in two steps:

  • Concatenate the percent symbols before the statement. For example, when you read the value:

    $q = "%" . $_POST['q'] . "%";
    
  • Remove the percentages and quotes in the query, simplifying the like :

    ... where ar.pdf LIKE ?
    
  • With those changes the code interprets the "?" as a bookmark and it should work for you:

    <?php
    
        $conexion = new mysqli('localhost','','','');
        if (mysqli_connect_error()) { echo mysqli_connect_error(); exit; }
        $q = "%" . $_POST['q'] . "%";
        $consulta= $conexion->prepare("SELECT ar.Titulo_Articulo, ar.Id_Articulo, r.Id_Revista FROM articulos as ar INNER JOIN revista as r on r.Id_Revista = ar.Id_Revista where ar.pdf LIKE ?");
        $consulta->bind_param("s", $q);
        $consulta->execute();
        if($conexion->affected_rows>0)
        {
          while($fila=$res->fetch_array())
            {
    
                echo '<a href="autenticacion.php?id='.$fila["Id_Revista"].'&articulo='.$fila["Id_Articulo"].'" width="50%" class="sugerencias" onclick="myFunction2('.utf8_encode($fila["Titulo_Articulo"]).')"><br>'.utf8_encode($fila['Titulo_Articulo']).'</a>';
    
            }
        }
        else
        {
          echo '<b>No hay sugerencias</b>';
        }
        $consulta->close();
        $conexion->close();
    
        
    answered by 21.04.2016 в 22:25