【发布时间】:2016-02-20 01:53:05
【问题描述】:
您好,我正在尝试从我的 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;
}
}
然后当我试图理解它的时候->
警告:D 行 13 中的非法字符串偏移 'post_title'。
为什么要这样做以及如何解决它???
【问题讨论】:
-
$post 似乎不是一个数组,可以返回 null。试试 var_dump($post);
-
抱歉,我编辑了 mysqli_fetch_assoc,我编辑了@Ekin :) 如果它在你放的地方