Jump to content

PHP Number of variables error


whojstall11

Recommended Posts

My code keeps giving me and error on 22 saying:

Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: Number of variables doesn't match number of parameters in prepared statement in E:\xampp\htdocs\m9\findRecords2.php on line 22

<html>
  <head>
    <title>Display Records</title>
  </head>
  <body>
    <?php
      $conn = new mysqli("localhost", "proxy_user", 
            "my*password", "m9");
      if (mysqli_connect_errno()){
          echo 'Cannot connect to database: ' . 
              mysqli_connect_error($conn);
      }
      else{
         //read keyword from user
         if(!empty($_POST["keyword"])){
            $keyword = $_POST["keyword"]; 
             // create prepared statement 
           if ($query = mysqli_prepare($conn,
          "SELECT FirstName, LastName, Age, Hometown, Job FROM people WHERE age <='" . $keyword ."' " )) {

       // bind parameters
       mysqli_stmt_bind_param ($query, "i", $keyword);
             
            //run the query and keep results in $result variable
            mysqli_stmt_execute($query);

            // bind variables to prepared statement 
            mysqli_stmt_bind_result($query, $FirstName, $LastName, $Age, $Hometown, $Job);

            // fetch values 
            while (mysqli_stmt_fetch($query)) {
                 echo "<strong>$LastName, $FirstName</strong> from $Hometown<br/>age: $Age, occupation: $Job <br/><br/>";
             }

           //free memory used by a result handle 
       mysqli_stmt_close ($query);
            } else //problem with a query
              echo "Error: " . mysqli_error($conn);
          } else { //no keyword 
              echo "No keyword was specified";
          }
         
         mysqli_close($conn);
     }
?>
  </body>
</html>

keyword and age are ints

can anybody please help

Link to comment
https://forums.phpfreaks.com/topic/259084-php-number-of-variables-error/
Share on other sites

 if ($query = mysqli_prepare($conn,
          "SELECT FirstName, LastName, Age, Hometown, Job FROM people WHERE age

With prepared statements you don't stick the values right into the query. For mysqli substitute the variable with a question mark. No quotes, even for strings.

Speaking of quotes, don't use quotes for numbers. Prepared statement or not.

[code=php:0] if ($query = mysqli_prepare($conn,
          "SELECT FirstName, LastName, Age, Hometown, Job FROM people WHERE age

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.