0
$sql = "SELECT images.id, title, image, note 
        FROM news 
        INNER JOIN images ON news.id = images.id_news
        WHERE news.id=?
        ORDER BY images.id DESC";

$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));

$e = $stmt->fetch();

foreach($e[] as $row)
{
    $e[]=array(
        'title'=>$row['title'],
        'image'=>$row['image'],
        'note'=>$row['note']
        );
}

然后我有

Warning: Illegal string offset 'title' in C:\xampp\htdocs\Wazup.mn\inc\functions.inc.php on line 20

Warning: Illegal string offset 'image' in C:\xampp\htdocs\Wazup.mn\inc\functions.inc.php on line 21

Warning: Illegal string offset 'note' in C:\xampp\htdocs\Wazup.mn\inc\functions.inc.php on line 22
4

2 回答 2

0

省略语句[]中的foreach

foreach($e as $key => $val)
{
    $key=$val
}

但是,更简单的是,你可以做

extract($e);
于 2013-09-12T12:31:58.373 回答
0

您可以在 fetchAll 函数中使用 FETCH_COLUMN,然后使用 foreach 循环

$e = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
foreach($e as $row)
{
    $e=array(
        'title'=>$row['title'],
        'image'=>$row['image'],
        'note'=>$row['note']
        );
}
于 2013-09-12T12:41:50.773 回答