无法从SQL数据库中获取帖子

时间:2016-02-20 01:53:05

标签: php html sql

您好我试图从我的sql获取数据是id,post_author,post_date,post_content,post_title& post_status

现在我对post.php的基础就是这个

<?php require_once("../../includes/session.php"); ?>
<?php require_once("../../includes/db_connection.php"); ?>
<?php require_once("../../includes/functions.php"); ?>
<?php
$post = "";
$id = 1;
// ^'will be using $_GET['id'] to get current post
find_post_by_id($id, $public=true);
?>
<?php include("../../includes/layouts/header.php") ?>

<div class="container post">
    <h1><?php echo htmlentities($post["post_title"]); ?></h1>
    <p>Auther & postdate n time</p>
    <hr class="hrpost">
    <div class="content">
        Content in form of the text
    </div>
</div>

<?php include("../../includes/layouts/footer.php") ?>

并且在functions.php中我有这个

function confirm_query($result_set) {
    if (!$result_set) {
        die("Database query failed.");
    }
}

function find_post_by_id($post_id, $public=true) {
    global $connection;

    $safe_page_id = mysqli_real_escape_string($connection, $post_id);

    $query  = "SELECT * ";
    $query .= "FROM posts ";
    $query .= "WHERE id = {$safe_page_id} ";
    if ($public) {
        $query .= "AND post_status = \"publish\" ";
    }
    $query .= "LIMIT 1";
    $post_set = mysqli_query($connection, $query);
    confirm_query($post_set);
    if($post = mysqli_fetch_assoc($post_set)) {
        return $post;
    } else {
        return null;
    }
}

然后当我试图得到它的说法 - &gt;

  

警告:非法字符串偏移&#39; post_title&#39;在D行13。

为什么要这样做以及如何解决它?

1 个答案:

答案 0 :(得分:0)

这可能就是你要找的东西。

$post = find_post_by_id($id, $public=true);

如果查询未返回您需要的内容,请确保打印出或记录查询。然后在你的mysql控制台上运行该查询以仔细检查它实际返回的内容。