0

Hi I have an error when I call a function.

"Warning: Illegal string offset 'id' in C:\xampp\htdocs\blog\posts.php on line 28 2"

function:

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')
        );
    }

Call:

require_once "functions.php";
    $_posts = get_short_posts();
    foreach($_posts as $_post) {
        echo $_post['id'];
    }
Settori
  • 3
  • 2

3 Answers3

1

You know you return after the first iteration in the get_short_posts function right, so the foreach will not work as expected.

Try:

<?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'];
}
?>

Also, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0
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;
    }

you return data, so the loop stops, save your data in a array and return that array like abive code

Gert B.
  • 2,282
  • 18
  • 21
0

Your code is wrong it should be as below, assuming the query is returning data as mentioned.

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'];
    }
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63