Home  >  Q&A  >  body text

The number of parameters in the prepared statement is inconsistent with the number of variables bound by bind_param

<p>Here is my code snippet: </p> <pre class="brush:php;toolbar:false;">$stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = '?' ORDER by model"); $stmt->bind_param('is', $year, $make); $stmt->execute();</pre> <p>When I output the values ​​for $year and $make, I can see the values, but when I run this script I get a null value and the following warning appears in my log file: </ p> <blockquote> <p>PHP Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement</p> </blockquote> <p>In this case, the type of year in the database is int(10), I have tried converting it to int type for passing, and make is a varchar(20) with utf8_unicode_ci encoding. Is there anything I'm missing? </p>
P粉578343994P粉578343994396 days ago402

reply all(1)I'll reply

  • P粉513316221

    P粉5133162212023-08-23 14:52:19

    Your prepared statement is wrong, it should be:

    $stmt = $mysqli->prepare("
        SELECT DISTINCT model FROM vehicle_types WHERE year = ? AND make = ? ORDER by model
    ");
    $stmt->bind_param('is', $year, $make);
    $stmt->execute();

    When you prepare a statement, you must replace each variable with a question mark instead of quotes. Question marks within quotes will not be recognized as placeholders.

    The number of question marks must be equal to the number of variables in bind_param()

    reply
    0
  • Cancelreply