1

I don't see any problem with my code and yet I still get this error in my code which doesn't make sense at all.

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in line 131

This is my full code:

$stmt = $db_conx->prepare("SELECT id, product_name FROM yt ORDER by id");
if ($stmt) {
    $product_list = "";
    $stmt->bind_param("is", $id, $product_name);
    if ( ! $stmt->execute() ) {
      die ( $stmt->error );
    }
    $stmt->bind_result($id, $product_name);
    $stmt->store_result();
    
    while ($stmt->fetch()) {
        $value[] = array('id'=>$id, 'product_name'=>$product_name);
        $product_list .= "<li class='mix ".$product_name."' data-name='".$product_name."'><a href='../product_images/" . $id . "Image1.jpg'>
                                            <img src='../product_images/" . $id . "Image1.jpg'></a>
                                            <h4>".$product_name."</h4>
                                            <br>
    
                                        </li>
                                        <div style='width:120px;'><a href='edit.php?edit=$id'>Edit this product</a></div>
    <br>
    <div style='width:120px;'><a href='products.php?delete=$id'>Delete this product</a></div>";
    
        $files = glob('../cache/*'); // get all file names
        foreach ($files as $file) { // iterate files
            if (is_file($file)) {
                unlink($file); // delete file
            }      
        }
    }
    mysqli_stmt_close($stmt);

And this is what there is on the Line 131:

$stmt->bind_param("is", $id, $product_name);

is there something that I am missing?

hakre
  • 193,403
  • 52
  • 435
  • 836
rooz
  • 361
  • 1
  • 8
  • 22

3 Answers3

3

You try to bind 2 parameters to a query that does not have any parameters:

$stmt = $db_conx->prepare("SELECT id, product_name FROM yt ORDER by id");
$stmt->bind_param("is", $id, $product_name);

You can only bind parameters if you define placeholders for them like this:

$stmt = $db_conx->prepare("SELECT id, product_name FROM where id = ? or  product_name = ?");
$stmt->bind_param("is", $id, $product_name);

The ? denotes placeholders you can bind parameters to.

maxhb
  • 8,554
  • 9
  • 29
  • 53
  • 1
    doesnt this have to be SELECT id, product_name FROM yt where id = ? or product_name = ? – rooz Feb 11 '16 at 11:24
  • Yeah, fixed it while you were commenting. Please see this just as an example which probably does not make too much sense :-) – maxhb Feb 11 '16 at 11:25
1

I got the same error, but my problem only was, that I used thicks, and I didn't noticed this for a while. Ofc, in this case my ? was a string and not a variable..., I hope it hepls someone, so use this:

SELECT * WHERE langCode = ?

instead of this:

SELECT * WHERE langCode = '?'

-1

you are binding the parameter "is" which doesn't exist in your sql statement edit: and you dont have any "?" placeholders either

msqli_stmt::bind_param

lonewolf217
  • 611
  • 4
  • 10
  • I thought that's what $id, $product_name is!? – rooz Feb 11 '16 at 11:22
  • http://php.net/manual/en/mysqli-stmt.bind-result.php bind-result collects data from the two columns you selected in your query. it does not bind a variable in the dynamic query. look at the docs for bind_param – lonewolf217 Feb 11 '16 at 11:25