1

I am having an extremely weird problem that does not make sense to me.

I am getting the "Illegal string offset" in my third variable that is part of the array in a foreach loop no matter what the data is pulling from. At first it thought it was an issue with my Date formatting but if I change it to another field it's the same error message only with that field name in place.

The code looks like this:

$posts = get_tableContents($con,'posts');
foreach($posts as $post)
{
    $postID = $post['id'];
    $post = $post['subject'];
    $mydate = "something to test";
    $datePosted = $post['date'];

    echo $mydate." <a class='community' href='displayNews.php?post=".$postID."'>".$post."</a>".$datePosted."<br />";
}

If I switch $datePosted with $post the same issue happens just changes the error to say subject instead of date.

Results: Warning: Illegal string offset 'subject' in D:\xampp\htdocs\snj\news.php on line 26 something to test 2014-09-09

Any thoughts would be appreciated.

Here is the function I am calling in case it is something in there:

function get_tableContents($con,$table)
{
    $results = array();
    $sql = mysqli_query($con, "SELECT * FROM $table")
        or die ("Error: " . mysqli_error($con));
    while($row = mysqli_fetch_array($sql)) 
    {
        $results[] = $row;    
    }

    return $results;
}
2
  • Are You sure u have subject key in your array? var_dump($posts); before foreach and after get_tableContents(); Commented Nov 9, 2014 at 17:04
  • You are re assigning $post in the loop.. Then try to use it like an array.. Call your variables something different t Commented Nov 9, 2014 at 17:13

1 Answer 1

1

Per my comment. In your loop you make $post equal to the post subject... And then try to pull out something else from post.

Aka $post = $post['subject']; Change that to$postSubject = $post['subject'];

Also check the case.. You may need to do $post['Subject'] in case the column name is a capital s on subject

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

2 Comments

ugggh! Silly me. It's been a long weekend and didn't catch that. Thanks so much John!
@jAC No problem!! Glad I could help. Thanks for the quick response and uovote. I'll reciprocate :)

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.