2
votes

Good day, I'm not really familiar with PHP and i get this error when i try to execute my query.

Warning:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /Users/site/userpanel.php on line 10

Code:

$query="SELECT SUM(gebruiker_id) AS totalitems FROM inventory WHERE gebruiker_id = 1";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $items);
$stmt->execute();
$result = $stmt->bind_result($col1);

Thanks in advance!

2
Mixing languages in code and/or database is considered a design flaw. - Sherlock

2 Answers

3
votes

You have to use a placeholder like this:

$query="SELECT SUM(gebruiker_id) AS totalitems FROM inventory WHERE gebruiker_id = ?";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $items);
$stmt->execute();
$result = $stmt->bind_result($col1);
1
votes

Sometimes you don't need to bind:

$query="SELECT SUM(gebruiker_id) AS totalitems FROM inventory WHERE gebruiker_id = 1";
$stmt = $db->prepare($query);
//$stmt->bind_param("s", $items);
$stmt->execute();
$result = $stmt->bind_result($col1);

or

$query="SELECT SUM(gebruiker_id) AS totalitems FROM inventory WHERE gebruiker_id = 1";
$stmt = $db->query($query);
$result = $stmt->bind_result($col1);

But if you were trying to bind $items to gebruiker_id then follow @rizier123 answer.