duanlei0282 2015-03-21 20:49
浏览 53
已采纳

PHP MySQLi编写的语句在绑定不同参数时给出相同的结果[重复]

This question already has an answer here:



I'm trying to make a class that I can use for showing my 5 most recent and 4 most popular posts. I'm doing this using a prepared statement, leaving the ORDER BY statement open for including two different parameters.

The problem is, when I run this, I get the same result for both (the posts come in the same order). The code I run in the main page is this:

<?php
    $test = new SideBar();
    echo $test->recent();
    echo $test->popular(); // returns the same as recent() for some reason
?>

And this is the class:

class SideBar
{
    // Storing the names of the database table columns
    private $id = 'n';
    private $rating = 'mean';
    private $stmt;
    private $result;

    public function __construct()
    {
        global $mysqli;
        $this->stmt = $mysqli->prepare("SELECT n,title,date
                                        FROM claims
                                        WHERE active = 1 
                                        ORDER BY ? DESC
                                        LIMIT 5");
    }

    public function recent()
    {
        $this->result = "";
        return $this->build($this->id);
    }

    public function popular()
    {   
        $this->result = "";
        return $this->build($this->rating);
    }

    private function build($order)
    {
        $this->stmt->bind_param('s',$order);
        $this->stmt->execute();
        $this->stmt->bind_result($n, $title, $date);
        while($this->stmt->fetch())
        {
        $this->result .= '<a href="[mydomain]?id='.$n.'">';
        $this->result .= $title.' '.$date;
        $this->result .= "</a>
";
        }
        return $this->result;
    }

    public function __destruct()
    {
        $this->stmt->close();
    }
}
</div>
  • 写回答

1条回答 默认 最新

  • douhe4336 2015-03-21 23:19
    关注

    Obviously you can't bind a column name to a an ORDER BY clause. Instead you can form the necessary query in the recent and popular functions and move the prepare pdo statement into the build function. Here are the modifications:

    public function __construct()
        {
        }
    
        public function recent()
        {
            $this->result = "";
            $q="SELECT n,title,date
                                            FROM claims
                                            WHERE active = 1 
                                            ORDER BY id DESC
                                            LIMIT 5"
            return $this->build($q);
        }
    
        public function popular()
        {   
            $this->result = "";
            $q="SELECT n,title,date
                                            FROM claims
                                            WHERE active = 1 
                                            ORDER BY mean DESC
                                            LIMIT 5"
            return $this->build($q);
        }
    
        private function build($query)
        {
            global $mysqli;
            $stmt = $mysqli->prepare($query)
            $stmt->execute();
            $stmt->bind_result($n, $title, $date);
            while($stmt->fetch())
            {
            $this->result .= '<a href="[mydomain]?id='.$n.'">';
            $this->result .= $title.' '.$date;
            $this->result .= "</a>
    ";
            }
            return $this->result;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化