只选择一条记录

时间:2015-10-02 10:44:21

标签: php pdo

这里在a.php文件中只显示了1行。我使用了fetch(PDO :: FETCH_ASSOC)stll我没有从chatlog获取整个行。 问题出在哪儿?

a.php只会

<html>
<head>
</head>
<body>
<?php
Include ('connect.php');
$query = $handler->prepare("SELECT message FROM chatlog");
$query->execute();
while ($row = $query->fetch()) {
    $message = $row['message'];
    $query = $handler->prepare("SELECT * FROM smilies"); 
    $query->execute();
    while ($row = $query->fetch()) { 
        $emoji = $row['emoji'];
        $smiley = "<img src='image/" . $row['smiley'] . "'/>";
        $message = str_replace($emoji, $smiley, $message); 
    }
    print_r("$message");
}
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

<?php
Include ('connect.php');

$smileys = $handler->query("SELECT emoji, smiley FROM smilies")->fetchAll(); 
$query   = $handler->query("SELECT message FROM chatlog");
foreach($query as $row)
{
    $message = $row['message'];
    foreach($smileys as $sm)
    { 
        $smiley  = "<img src='image/$sm[smiley]'/>";
        $message = str_replace($sm['emoji'], $smiley, $message); 
    }
    echo $message;
}