PHP-带数组的foreach函数

PHP-带数组的foreach函数,php,arrays,function,foreach,while-loop,Php,Arrays,Function,Foreach,While Loop,嗨,我调用函数时出错了 警告:C:\xampp\htdocs\blog\posts.php中的字符串偏移量'id'非法 第28行 2“ 功能: function get_short_posts() { $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) {

嗨,我调用函数时出错了

警告:C:\xampp\htdocs\blog\posts.php中的字符串偏移量'id'非法 第28行 2“

功能:

function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        return array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
    }
电话:


返回数据后,循环停止,将数据保存在一个数组中,并像abive代码一样返回该数组。

您知道,在get_short_posts函数的第一次迭代后返回,因此foreach将无法按预期工作

尝试:


也。它们不再得到维护。看到了吗?相反,学习,并使用,或-将帮助您决定哪一个。如果您选择PDO,.

您的代码是错误的,它应该如下所示,假设查询返回的是前面提到的数据

function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    $rows = array();
    $return_data = array();
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        $data = array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
        $return_data[] = $data;
    }
    return $return_data ;

}


require_once "functions.php";
    $posts = get_short_posts();
    foreach($posts as $key=>$val) {
        echo $val['id'];
    }

尝试执行
die(var_dump($row))在while循环中。这将停止所有其他执行,并在屏幕上显示错误。您知道,在get_short_posts函数的第一次迭代后返回,因此foreach将无法按预期工作。请显示您的表列名称。可能列“id”称为“id”或“id”?同样,请尝试:
$result=mysql\u query($sql)或die(mysql\u error())
function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        $data [] = array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );

    }
    return $data;
    }
<?php 
function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    $return = array();
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        $return[] = array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
    }
    return $return;
}
?>

<?php 
require_once "functions.php";

foreach(get_short_posts() as $_post) {
    echo $_post['id'];
}
?>
function get_short_posts() {
    $sql = "SELECT * FROM posts ORDER by id DESC LIMIT 0, 5";
    $result = mysql_query($sql);
    $rows = array();
    $return_data = array();
    while($row = mysql_fetch_assoc($result)) {
        $timestamp = new DateTime($row['date']);
        $data = array (
            "id" => $row['id'],
            "title" => $row['title'],
            "content" => $row['content'],
            "author" => $row['author'],
            "date" => $timestamp->format('d-m-Y'),
            "time" => $timestamp->format('H:i')
        );
        $return_data[] = $data;
    }
    return $return_data ;

}


require_once "functions.php";
    $posts = get_short_posts();
    foreach($posts as $key=>$val) {
        echo $val['id'];
    }