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'];
    }
4
  • Try doing die(var_dump($row)); in your while loop. This will halt all other execution, and display the error on screen. Commented Jan 18, 2014 at 21:15
  • 2
    You know you return after the first iteration in the get_short_posts function right, so the foreach will not work as expected. Commented Jan 18, 2014 at 21:15
  • 1
    Show your table columns names, please. Maybe column "id" called "Id" or "ID"? Commented Jan 18, 2014 at 21:15
  • Also, try: $result = mysql_query($sql)or die(mysql_error()); Commented Jan 18, 2014 at 21:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.