想要做产品过滤,但我得到了错误

时间:2019-06-18 22:38:39

标签: php mysql

我正在做产品过滤器应用程序,但出现此错误

  

致命错误:未捕获错误:调用未定义方法mysqli_stmt :: fetchAll()

这是我的代码:

 <div class="list-group">
                    <h3>Grupe</h3>
                    <div style="height: 180px; overflow-y: auto; overflow-x: hidden;">
                    <?php
                    include_once("dbconfig.php");
                    $query = "SELECT DISTINCT(grupa) FROM slike  ORDER BY podgrupa DESC";
                    $statement = $mysqli->prepare($query);
                    $statement->execute();
                    $result = $statement->fetchAll();
                    foreach($result as $row)
                    {
                    ?>
                    <div class="list-group-item checkbox">
                        <label><input type="checkbox" class="common_selector brand" value="<?php echo $row['grupa']; ?>"  > <?php echo $row['grupa']; ?></label>
                    </div>
                    <?php
                    }

                    ?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
$host="localhost";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="slikezasajt";

$mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket)
    or die ('Could not connect to the database server' . mysqli_connect_error());

//$mysqli->close();
?>
</body>
</html>

上面是我的dbconfig文件。

2 个答案:

答案 0 :(得分:0)

尝试此代码。我已经对其进行了测试,它可以在我的本地笔记本电脑上工作。

  <?php
    $host="localhost";
    $port=3306;
    $socket="";
    $user="root";
    $password="";
    $dbname="slikezasajt";   
    $mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket)
        or die ('Could not connect to the database server' . mysqli_connect_error());



      $query = "SELECT DISTINCT(grupa) FROM slike  ORDER BY podgrupa DESC";
      $statement = $mysqli->query($query);




    ?>



<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
   <div class="list-group">
    <h3>Grupe</h3>
    <div style="height: 180px; overflow-y: auto; overflow-x: hidden;">
    <?php


   while($row = $statement->fetch_assoc())
    {
    ?>
    <div class="list-group-item checkbox">
        <label><input type="checkbox" class="common_selector brand" value="<?php echo $row['grupa']; ?>"  > <?php echo $row['grupa']; ?></label>
    </div>
    <?php
    }

    ?>
</body>
</html>

答案 1 :(得分:0)

使用以下代码更新您的代码:

 <div class="list-group">
                    <h3>Grupe</h3>
                    <div style="height: 180px; overflow-y: auto; overflow-x: hidden;">
                    <?php
                    include_once("dbconfig.php");
                    $query = "SELECT DISTINCT(grupa) FROM slike  ORDER BY podgrupa DESC";
                    $statement = $mysqli->prepare($query);
                    $statement->execute();
                    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
                    foreach($result as $rows =>$row)
                    {
                    ?>
                    <div class="list-group-item checkbox">
                        <label><input type="checkbox" class="common_selector brand" value="<?php echo $row['grupa']; ?>"  > <?php echo $row['grupa']; ?></label>
                    </div>
                    <?php
                    }

                    ?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
$host="localhost";
$port=3306;
$socket="";
$user="root";
$password="";
$dbname="slikezasajt";

$mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket)
    or die ('Could not connect to the database server' . mysqli_connect_error());

//$mysqli->close();
?>
</body>
</html>