试图获取非对象的属性/在布尔值上调用成员函数bind_param() [英] Trying to get property of non-object / Call to a member function bind_param() on boolean

查看:52
本文介绍了试图获取非对象的属性/在布尔值上调用成员函数bind_param()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问了很多遍了,但是解决方案似乎对我不起作用.

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()){

这段代码在if语句(试图获取非对象的属性)中给了我以下错误. var_dump()返回bool(false).因此,查询似乎是一个问题,但就我而言,这还可以.我检查了一下,没有错别字,所有的名字也都是正确的.

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.

我也试图这样做:

            $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();

但是它给了我一个不同的错误(在布尔值上调用成员函数bind_param()),因此再次看来查询是一个问题.我真的不知道该如何处理.有什么建议吗?

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?

更新:

                $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>
                            ";
                }
            }

推荐答案

在此处阅读:

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

问题是您的SQL,您需要使用?代替变量名并准备绑定数据.我相信你想要

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

这篇关于试图获取非对象的属性/在布尔值上调用成员函数bind_param()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆