2

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!

1
  • Mixing languages in code and/or database is considered a design flaw. Commented Jan 11, 2015 at 17:13

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help! if i use ? as placeholder result will be 1 while it has to be 8 because gebruiker_id is in there 8 times
You to man! only it didn't fix my issue. it should return 8 as there are 8 rows in my table with gebruiker_id 1.
@J.Koppen And you are sure that $items is 1 and you also did print_r($result);? And i think you want to use count()
when i print $items it gives me nothing and when i print $result it gives me 1
@J.Koppen Did you even defined $items anywhere?!
|
1

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.

2 Comments

Let me explain this. I have to sum up how many times "gebruiker_id" 1 is in the table "items" (which is 8) and then result has to be 8.
@J.Koppen use COUNT()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.