PHP PDO echo SELECT语句

时间:2013-11-21 20:17:00

标签: php mysql pdo

我想要回显PHP文件中的SELECT语句。

它没有抛出异常,但它没有从查询中抛出正确的预期总数,它向我显示1这是奇怪的。

<?php require("config.php"); 
    $stmt = $db->prepare("SELECT COUNT( * ) AS total_record FROM  `markers` WHERE fishspecies =  'Bass'");
    echo $stmt ->execute(); 
?>

2 个答案:

答案 0 :(得分:2)

$stmt->execute();会返回真实或虚假的值。

从数据库返回行。

您需要使用fetchfetchAll等来获取数据库中的值。

答案 1 :(得分:0)

<?php 
    require("config.php"); 
    $stmt = $db->prepare("SELECT COUNT( * ) AS total_record FROM  `markers` WHERE fishspecies =  'Bass'");
    $stmt->execute();
    $records = $stmt->fetchAll();

    //Specify the col_name
    foreach($records as $record)
    {
        echo $record['col_name'];
    }

    //Auto Print
    foreach($records as $col => $val)
    {
        echo "Column Name: $col - Value: $val <br/>";
    }

?>
相关问题