在 php 中使用 bind_param() 时出错
error when using bind_param() in php
我在使用 bind_param()
时一直收到跟随错误,我已经尝试了所有方法来修复它,但似乎没有任何效果。
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
这是我的代码
$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>';
}
}
}
您需要将该值绑定到查询字符串中的占位符 ?
。然后你需要查看传递给 bind_param()
的参数——第一个参数应该是变量的类型——在这种情况下,$search
只是一个字符串,所以第一个参数应该是 s
.
此外,您应该注意 $stmt->num_rows
是一个 属性,而不是一个方法。这个 属性 可能不准确(也就是说,它可能在获取结果之前显示零行)除非您首先存储结果,首先使用 $stmt->store_result()
。这两个都需要在 执行之后。
然后您需要使用bind_param()
绑定结果。这意味着绑定查询编辑的每一列 select。因此,最好 select 您要查找的特定列,而不是 SELECT *
。当您现在获取时,它应该是提供给 while
的单个参数,而不是将其分配给变量。
$search = "%$search%";
if ($stmt = $db->prepare("SELECT name, location, gender, date_of_birth, url FROM Users WHERE name LIKE ?")){
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->bind_result($name, $location, $gender, $date_of_birth, $picture);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count == 0) {
$output = "There was no search results!";
} else {
while ($stmt->fetch()) {
// You can use $name, $location, $gender, $date_of_birth, $picture here
$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>';
}
}
}
我在使用 bind_param()
时一直收到跟随错误,我已经尝试了所有方法来修复它,但似乎没有任何效果。
Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables
这是我的代码
$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>';
}
}
}
您需要将该值绑定到查询字符串中的占位符 ?
。然后你需要查看传递给 bind_param()
的参数——第一个参数应该是变量的类型——在这种情况下,$search
只是一个字符串,所以第一个参数应该是 s
.
此外,您应该注意 $stmt->num_rows
是一个 属性,而不是一个方法。这个 属性 可能不准确(也就是说,它可能在获取结果之前显示零行)除非您首先存储结果,首先使用 $stmt->store_result()
。这两个都需要在 执行之后。
然后您需要使用bind_param()
绑定结果。这意味着绑定查询编辑的每一列 select。因此,最好 select 您要查找的特定列,而不是 SELECT *
。当您现在获取时,它应该是提供给 while
的单个参数,而不是将其分配给变量。
$search = "%$search%";
if ($stmt = $db->prepare("SELECT name, location, gender, date_of_birth, url FROM Users WHERE name LIKE ?")){
$stmt->bind_param("s", $search);
$stmt->execute();
$stmt->bind_result($name, $location, $gender, $date_of_birth, $picture);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count == 0) {
$output = "There was no search results!";
} else {
while ($stmt->fetch()) {
// You can use $name, $location, $gender, $date_of_birth, $picture here
$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>';
}
}
}