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;
}
var_dump($posts);before foreach and after get_tableContents();