如何在PHP中使用SQL从数据库中获取字符串值?

时间:2014-04-23 04:19:47

标签: php mysql sql database

所以我需要在数据库中有一个表项表示。 FRUIT表有四个值:ID,TYPE,PRICE,DESCRIPTION。我想循环迭代FRUIT表中的每个项目并将其放在一个表中。这就是我到目前为止所做的:

echo "<table border=\"1\"><tr><th>Type</th><th>Price</th><th>Description</th></tr>";
$invSize = $dbh->prepare("SELECT COUNT(*) FROM FRUIT;");
for ($count = 0; $count < $invSize; ++$count){
        $type = $dbh->prepare("SELECT TYPE FROM FRUIT WHERE ID = $count;");
        $price = $dbh->prepare("SELECT PRICE FROM FRUIT WHERE ID = $count;");
    $desc = $dbh->prepare("SELECT DESCRIPTION FROM FRUIT WHERE ID = $count;");
    echo "<tr><td>", $type, "</td><td>", $price, "</td><td>", $desc, "</td></tr>";
}
echo "</table>";

当我在服务器上运行代码时,它显示表的第一行(标题),然后在第一个单元格的第二行显示此错误消息:

Catchable fatal error: Object of class PDOStatement could not be converted to string.

知道为什么会这样,以及我可以做些什么来修复它?

0 个答案:

没有答案