PDO不显示结果

时间:2016-05-20 15:25:44

标签: php pdo

这是我的第一个问答。

我无法显示$ row ['id'] ['name'] ['level']的结果 IT只显示 ID: 名称: LEVEL:

但结果出现了

我尝试将PDO :: FETCH_ASSOC添加到FETCH(),但是它给出了预期为0的1个参数

require_once 'connection.php';
$query = "SELECT * FROM members";
$stmt = $cnx->prepare($query);
$stmt->execute();

while($row = $stmt->fetch()) {
echo "Id: " . $row['id'] . "<br>";
echo "Name: " . $row['name'] . "<br>";
echo "Level: " . $row['level'] . "<hr>";
}

1 个答案:

答案 0 :(得分:0)

当您使用mysqli api时,使用fetch会有点复杂:

$stmt = $cnx->prepare($query);
$stmt->execute();

// first you need to bind result to variables:
$stmt->bind_result($id, $name, $level)
// also make sure that you bind the SAME number 
// of variables as number of fields selected in a query

while($stmt->fetch()) {
    echo "Id: " . $id . "<br>";
    echo "Name: " . $name . "<br>";
    echo "Level: " . $level . "<hr>";
}