I keep getting the follow error when using bind_param()
and have tried everything to fix it but nothing seems to work.
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
here is my code
$output = '';
$output2 = '';
$output3 = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
if ($stmt = $db->prepare("SELECT * FROM Users WHERE name LIKE '%$search%'")){
$stmt->bind_param("s", $search);
$count = $stmt->num_rows();
$stmt->execute();
if($count == 0){
$output = "There was no search results!";
}else{
while ($rows = $stmt->num_rows) {
$name = $row ['name'];
$location = $row ['location'];
$gender = $row ['gender'];
$date_of_birth = $row ['date_of_birth'];
$picture = $row['url'];
$output .='<form action="header.php" method="post"><div class="row"><div class="col-sm-3">'.$name.'<br>'.$location.'<br>'.$gender.'<br>'.$date_of_birth.'</div>';
$output2 = '<div class="col-sm-3"><img src="upload/'.$picture.'"width="180" height="144" /></div></div>';
$output3 = '<input id="add_friend" name= "addfriend" type="submit" value="Add As Friend" /></form>';
}
}
}
You need to bind the value to a placeholder
?
in the querystring. Then you need to look at the parameters you pass tobind_param()
- that first argument should be the types of variables - in this case,$search
is just one string, so the first argument should bes
.Further, you should note that
$stmt->num_rows
is a property, not a method. This property may be inaccurate (that is, it might show zero rows before the results are fetched) unless you store the results first, by using$stmt->store_result()
first. Both of these needs to come after executing.Then you need to bind the results using
bind_param()
. This means binding every column selected by the query. Therefor, it's better to select the specific columns you are looking for, instead of doingSELECT *
. When you now fetch, it should be the single argument supplied towhile
, without assigning it to a variable.mysqli_stmt::bind_param()
mysqli_stmt::bind_result()
mysqli_stmt::num_rows
mysqli_stmt::store_result()