试图在布尔值上获取非对象/调用成员函数bind_param()的属性

时间:2022-01-23 20:35:52

I am aware that this question has been asked many times but the solutions doesn't seem to be working for me.

我知道这个问题已被多次询问,但解决方案似乎对我不起作用。

I am writing a simple blog and I wanted to make an archive section, but I got stuck. This is a piece of code that I have and that is troublesome:

我正在写一个简单的博客,我想制作一个存档部分,但我陷入困境。这是我的一段代码,这很麻烦:

            $month = $_GET['month'];
            $year = $_GET['year'];

            $from = date('Y-m-01 00:00:00', strtotime("$year-$month"));
            $to = date('Y-m-31 23:59:59', strtotime("$year-$month"));

            echo $from;
            echo $to;

            $sql = "SELECT * FROM posts WHERE date >= ".$from." AND date <= ".$to." ORDER BY date DESC";
            $stmt = $conn->query($sql);
            var_dump($stmt);

            if($stmt->num_rows > 0){
                while($row = $stmt->fetch_assoc()){

And this piece of code is giving me the following error in the if statement (Trying to get property of non-object). The var_dump() returns bool(false). So it seems that the query is a problem, but as far as I'm concerned it is okay. I checked it and there is not typos and all names are correct too.

这段代码在if语句中给出了以下错误(试图获取非对象的属性)。 var_dump()返回bool(false)。所以看起来查询是一个问题,但就我而言,它是可以的。我查了一下,没有拼写错误,所有名字都是正确的。

I also tried to do it like this:

我也尝试这样做:

            $month = $_GET['month'];
            $year = $_GET['year'];

            $from = date('Y-m-01 00:00:00', strtotime("$year-$month"));
            $to = date('Y-m-31 23:59:59', strtotime("$year-$month"));

            echo $from;
            echo $to;

            $sql = "SELECT * FROM posts WHERE date >= ".$from." AND date <= ".$to." ORDER BY date DESC";
            $stmt = $conn->prepare($sql);
            var_dump($stmt);
            $stmt->bind_param('ii', $from, $to);
            $stmt->execute();

But it gives me a different error (Call to a member function bind_param() on boolean) so again it seems that the query is a problem. I don't really know how to deal with this. Any suggestions?

但它给了我一个不同的错误(在布尔上调用成员函数bind_param())所以再次看起来查询是一个问题。我真的不知道如何处理这件事。有什么建议么?

UPDATE:

更新:

                $month = $_GET['month'];
            $year = $_GET['year'];

            //set from and to dates
            $from = date('Y-m-01 00:00:00', strtotime("$year-$month"));
            $to = date('Y-m-31 23:59:59', strtotime("$year-$month"));

            echo $from;
            echo $to;

            $sql = "SELECT * FROM posts WHERE date >= ? AND date <= ? ORDER BY date DESC";
            $stmt = $conn->prepare($sql);
            //var_dump($stmt);
            $stmt->bind_param('ii', $from, $to);
            $stmt->execute();

            if($stmt->num_rows > 0){
                while($row = $stmt->fetch_assoc()){
                    $query = "SELECT * FROM comments WHERE postId=".$row['id'];
                    $comNumber = $conn->query($query);
                    $total = $comNumber->num_rows;


                    echo "<section class='post'>
                                <img class='postImage' src='img/".$row['id'].".png'>
                                <div class='postComment'>
                                    <div class='postCommentBubble'><img src='img/commentBubble.svg'></div>
                                    <div class='postCommentCount'>".$total." comments</div>";

                    echo "<div class='postDate'>".date("d F Y G:i", strtotime($row["date"]))."</div>";

                        if(isset($_SESSION['user']) && $_SESSION['privileges']==true){
                            echo "<a class='postDelete' href='delete-post.php?id=".$row["id"]."'><img src='img/garbage.svg'></a>";
                            echo "<a class='postEdit' href='edit-post.php?id=".$row["id"]."'><img src='img/pencil.svg'></a>";
                        }

                        echo "</div>
                                <header class='postTitle'><a href='post.php?id=".$row['id']."'>".$row['title']."</a></header>
                                <div class='postIntro'>".$row['intro']."</div>
                                <div class='postLinkContainer'><a class='postLink' href='post.php?id=".$row['id']."'>read more</a></div>
                            </section>
                            ";
                }
            }

2 个解决方案

#1


1  

Have a read here: http://php.net/manual/en/mysqli.prepare.php prepare you get a false if it fails. Hence your error.

请在此处阅读:http://php.net/manual/en/mysqli.prepare.php如果失败,请准备好。因此你的错误。

The issue is your SQL your need to use ? in place of variable names and prepare expects the data to bind. I believe you want:

问题是您需要使用SQL吗?代替变量名称和prepare需要绑定数据。我相信你想:

$sql = "SELECT * FROM posts WHERE date >= ? AND date <= ? ORDER BY date DESC";
$stmt->bind_param('ii', $from, $to);

#2


0  

In regards to your updated status, fetch_assoc() is not a member of a mysqli statement object. However, fetch() is and is the member function you are looking for.

关于更新状态,fetch_assoc()不是mysqli语句对象的成员。但是,fetch()是并且是您正在寻找的成员函数。

You can also use bind_result() to access the data.

您还可以使用bind_result()来访问数据。

So an example would look something like:

所以一个例子看起来像:

    $timestamp = time();

    //create select statement
    $stmt = $con->prepare("SELECT column_x, column_y, column_z FROM table_name WHERE date < ?");

    //bind $timestamp to the prepared statement
    $stmt->bind_param("i", $timestamp);
    $stmt->execute();

    //bind each selected column to a variable for iteration
    $stmt->bind_result($column_x, $column_y, $column_z);
    while($stmt->fetch())
    {
        echo $x . " " . $y . " " . $z . "\n";
    }

Here is the reference link for the mysqli_stmt class

这是mysqli_stmt类的参考链接

edit: I would also like to point out that while a mysli_stmt object does have a num_rows property, it is not the same as the num_rows property on a query result.

编辑:我还想指出,虽然mysli_stmt对象确实有num_rows属性,但它与查询结果中的num_rows属性不同。

#1


1  

Have a read here: http://php.net/manual/en/mysqli.prepare.php prepare you get a false if it fails. Hence your error.

请在此处阅读:http://php.net/manual/en/mysqli.prepare.php如果失败,请准备好。因此你的错误。

The issue is your SQL your need to use ? in place of variable names and prepare expects the data to bind. I believe you want:

问题是您需要使用SQL吗?代替变量名称和prepare需要绑定数据。我相信你想:

$sql = "SELECT * FROM posts WHERE date >= ? AND date <= ? ORDER BY date DESC";
$stmt->bind_param('ii', $from, $to);

#2


0  

In regards to your updated status, fetch_assoc() is not a member of a mysqli statement object. However, fetch() is and is the member function you are looking for.

关于更新状态,fetch_assoc()不是mysqli语句对象的成员。但是,fetch()是并且是您正在寻找的成员函数。

You can also use bind_result() to access the data.

您还可以使用bind_result()来访问数据。

So an example would look something like:

所以一个例子看起来像:

    $timestamp = time();

    //create select statement
    $stmt = $con->prepare("SELECT column_x, column_y, column_z FROM table_name WHERE date < ?");

    //bind $timestamp to the prepared statement
    $stmt->bind_param("i", $timestamp);
    $stmt->execute();

    //bind each selected column to a variable for iteration
    $stmt->bind_result($column_x, $column_y, $column_z);
    while($stmt->fetch())
    {
        echo $x . " " . $y . " " . $z . "\n";
    }

Here is the reference link for the mysqli_stmt class

这是mysqli_stmt类的参考链接

edit: I would also like to point out that while a mysli_stmt object does have a num_rows property, it is not the same as the num_rows property on a query result.

编辑:我还想指出,虽然mysli_stmt对象确实有num_rows属性,但它与查询结果中的num_rows属性不同。