使用以下代码

时间:2016-02-09 12:02:52

标签: php mysql pdo

我已经使用此代码从表tbl_users中选择数据,但它显示的是1.它是什么意思..

 require_once('config.php');
 $dbCon = getConnection();


 $sql = "SELECT * FROM tbl_users";
 $stmt = $dbCon->prepare($sql);


 print($stmt->execute());

任何人都可以帮忙...... ????

3 个答案:

答案 0 :(得分:0)

如果查询成功运行,$stmt->execute()会返回true,其将打印为1

要返回数据:

//Returns first row as array
$row = $stmt->fetch();

//Returns first row as key => value array
$row = $stmt->fetch(PDO::FETCH_ASSOC);

//Returns all rows as key=>value arrays
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

您可以使用它来打印如下数据:

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

//Print all data
$print_r($rows);

//Print data row-by-row
foreach($rows as $row){
    print_r($row);
}

答案 1 :(得分:0)

此时您正在打印查询的响应,而不是结果。

要打印结果,您应该执行以下操作:

foreach ($stmt as $row) {
        print $row['name'] . "\t";
        print $row['first_name'] . "\t";
        print $row['birth_date'] . "\n";
    }

答案 2 :(得分:0)

成功执行query execute()返回True

$sql = "SELECT * FROM tbl_users";
$stmt = $dbCon->prepare($sql);

$result = $stmt->fetchAll();
print_r($result);