Error in PHP code
Posted: Tue Feb 14, 2012 8:13 pm
Hi,
I have the following code, connectdb() is my function which connects to a db,
and this is outputting the error,
Fatal error: [] operator not supported for strings in /home/sifelou1/public_html/index2.php on line 31
line 31 is $description[] = $row['description'];, when I remove this the code works perfectly. It is stored as a varchar in the database and so are the news and image columns. I really can't understand why it isn't working, any help would be appreciated! Thanks in advance.
I have the following code, connectdb() is my function which connects to a db,
Code: Select all
$dbh = connectdb('mydb);
$sql = "SELECT title,description,img FROM news ORDER BY id DESC LIMIT 4";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $row)
{
$news[] = $row['title'];
$img[] = $row['img'];
$description[] = $row['description'];
}
$dbh = null;
Fatal error: [] operator not supported for strings in /home/sifelou1/public_html/index2.php on line 31
line 31 is $description[] = $row['description'];, when I remove this the code works perfectly. It is stored as a varchar in the database and so are the news and image columns. I really can't understand why it isn't working, any help would be appreciated! Thanks in advance.