简体   繁体   English

将PHP函数的返回值输入SQL数据库

[英]Input PHP Function Returned Values to SQL Database

I have this function: 我有这个功能:

public function post(){
    try{

        $con = connectDB();
        $sth = $con->prepare('SELECT * FROM posts');
        $sth->execute();
        $posts = $sth->fetchAll();

        foreach($posts as $post){ ?>

            <div>
                <h2><?php echo $post['title']; ?></h2>
                <p>
                    <?php echo $post['content']; ?>
                </p>
                <h6><?php echo $post['category']; ?></h6>
            </div>

        <?php }

    }catch(PDOException $e){echo $e->getMessage();}
}

And I want the return values of this function be stored in my database using my other function: 我希望使用其他函数将该函数的返回值存储在数据库中:

$page = new PageModel();
$post = new PostModel();

$page->addPage('Blog',$post->post());

But this one is not working. 但这是行不通的。 Can someone tell me what's wrong and if possible, suggest the most simple, possible way? 有人可以告诉我什么地方出了问题,如果可能的话,建议最简单,可行的方法吗? Thanks! 谢谢!

Return the values to retrieve from other area 返回值以从其他区域检索

public function post(){
    try{

        $con = connectDB();
        $sth = $con->prepare('SELECT * FROM posts');
        $sth->execute();
        $posts = $sth->fetchAll();

        foreach($posts as $post){ ?>

            <div>
                <h2><?php echo $post['title']; ?></h2>
                <p>
                    <?php echo $post['content']; ?>
                </p>
                <h6><?php echo $post['category']; ?></h6>
            </div>

        <?php }

    }catch(PDOException $e){echo $e->getMessage();}

return $posts;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM