Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用bind_param()时出错_Php_Mysqli_Bindparam - Fatal编程技术网

在php中使用bind_param()时出错

在php中使用bind_param()时出错,php,mysqli,bindparam,Php,Mysqli,Bindparam,在使用bind_param时,我一直遇到以下错误,并尝试了各种方法来修复它,但似乎没有任何效果 警告:mysqli_stmt::bind_param:类型定义字符串中的元素数与绑定变量数不匹配 这是我的密码 $output = ''; $output2 = ''; $output3 = ''; if(isset($_POST['search'])) { $search = $_POST['search']; $search = preg_replace("#[^0-9a-z

在使用bind_param时,我一直遇到以下错误,并尝试了各种方法来修复它,但似乎没有任何效果

警告:mysqli_stmt::bind_param:类型定义字符串中的元素数与绑定变量数不匹配

这是我的密码

$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\u result first先存储结果。这两项都需要在执行之后进行

然后需要使用bind_param绑定结果。这意味着绑定查询选择的每一列。因此,最好选择要查找的特定列,而不是执行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>';
         }
    }
}

您没有占位符,但绑定了一个值。请对此进行解释。请打开manual.RTFM并使用?而不是字符串插值。仍然得到相同的错误感谢您的解释,但我现在似乎得到以下错误致命错误:最大执行时间超过30秒,而$rows=$stmt->num\u rows仍然存在{代码中的某个地方?因为如果$stmt->num_rows大于零,它将运行到无穷大。我仍然有它,我现在已经取出它,它已经修复了问题,谢谢你的帮助