Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO-如何解决此问题?_Php_Sql_Pdo - Fatal编程技术网

PHP PDO-如何解决此问题?

PHP PDO-如何解决此问题?,php,sql,pdo,Php,Sql,Pdo,我正在遵循一本关于PHP的书中的代码,但我陷入了以下问题:它显示了我数据库中的帖子,但当我单击“阅读更多”链接时,它告诉我它找不到URL。下面是代码 任何关于错误的建议都将不胜感激 posts.php list-posts.php single-post.php 您正在将URL字符串硬编码到本地主机-文件是否位于指定的位置?http://localhost/other/posts.php 您可能应该使用相对路径而不是绝对路径。您已经在上硬编码了URLhttp://localhost 仅在运行服务

我正在遵循一本关于PHP的书中的代码,但我陷入了以下问题:它显示了我数据库中的帖子,但当我单击“阅读更多”链接时,它告诉我它找不到URL。下面是代码

任何关于错误的建议都将不胜感激

posts.php

list-posts.php

single-post.php


您正在将URL字符串硬编码到本地主机-文件是否位于指定的位置?http://localhost/other/posts.php 您可能应该使用相对路径而不是绝对路径。您已经在上硬编码了URLhttp://localhost 仅在运行服务器的计算机上工作。删除它并使用相对URL。您应该检查URL中的id是否也是有效的,我相信是的,我的意思是,当我运行…/other/posts.php时,它会显示我所有帖子id+内容的列表。然而,当我点击ReadMore时,它会给我一个URL not found消息。当我点击我的第一篇博文阅读更多链接id=1时,这是我的浏览器显示给我的URL:?id=1。我不完全确定你所说的相对/绝对路径是什么意思。请跟我说。我会用谷歌搜索你在查看初始列表时使用的完整URL是什么?Hobo Sapiens,我会找出如何使用相对URL。谢谢你的评论。是的,是的
<?php
Class Posts {
    public $db = '';
    public function __construct(){

        $this->db = new PDO("mysql:host=localhost;dbname=testposts", "username", "password");
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->index(); 

    }

    public function index(){
        $id = 0;
        $posts = array();
        $template = '';
        if (!empty($_GET['id'])){
            $id = $_GET['id'];
        }

        try {
            if (!empty($id)){
                $query = $this->db->prepare("SELECT * FROM posts WHERE id = ?");
                $params = array($id);
                $template = 'single-post.php';
            } else {
                $query = $this->db->prepare("SELECT * FROM posts");
                $params = array();
                $template = 'list-posts.php';
            }
            $query->execute($params);
            for ($i = 0; $row = $query->fetch(); $i++) {
                $posts[] = array('id' => $row['id'], 'content' => $row['content']);
            }
        } catch (PDOException $e){
            $e->getMessage();
        }
        $query->closeCursor();
        $db = null;
        require_once($template);
    }
}

$posts = new Posts();

?>
 <h1>List of Blog Posts</h1>
    <?php foreach ($posts as $post): ?>
        <h3>Post # <?php echo htmlspecialchars($post['id']); ?></h3>
        <?php echo htmlspecialchars($post['content']); ?>
        <a href="http://localhost/other/posts.php?id=<?php 
            echo htmlspecialchars($post['id']); ?>">Read More</a>
        <hr>
    <?php endforeach; ?>
<?php foreach ($posts as $post): ?>
    <h1>Post #<?php echo htmlspecialchars($post['id']); ?></h1>
    <hr>
    <?php echo htmlspecialchars($post['content']); ?>
    <a href="http://localhost/other/posts.php">Back to Post List</a>
<?php endforeach; ?>