PHP将函数查询改为不调用函数的直接查询


PHP Change Function Query to be Direct Query Without Call The Function

我有一个PHP代码函数get_news()

get_news函数

function get_news(){
    if($result = $this->db->query('SELECT * FROM tb_post WHERE post_id<>1 ORDER BY post_date DESC LIMIT 50')){
        $return = '';
        while($r = $result->fetch_object()){
            $return .= '<p>id: '.$r->post_id.' | '.htmlspecialchars($r->post).'</p>';
        }
        return $return;
    }
}

现在,我有1个php文件用于获取数据

if(isset($_POST) && !empty($_POST['counter']) && (int)$_POST['counter']!=$data['current']){
//the counters are diffrent so get new message list
$data['news'] = '';
$data['news'] .= $db->get_news();
$data['update'] = true;
}

我的问题是,如何在不调用函数的情况下将$db->get_news();更改为直接查询?

Try this
$news = '';
if($result = $this->db->query('SELECT * FROM tb_post WHERE post_id<>1 ORDER BY post_date DESC LIMIT 50')){
        $return = '';
        while($r = $result->fetch_object()){
            $news .= '<p>id: '.$r->post_id.' | '.htmlspecialchars($r->post).'</p>';
        }
    }
$data['news'] = $news;
$data['update'] = true;