如何在php变量中放置PDO查询的结果选择

时间:2014-05-26 03:27:21

标签: php mysql pdo

在我的项目中,我使用PDO连接器来访问我的mysql数据库。

$conexao = new PDO($dsn, $user, $pass);

我需要过滤器来获取注册用户表的名称usuarios。为此,我启动了一个session_start()并命令他将邮件放入查询中。

我做这样的事情:

<?php
session_start();
require_once 'configuracao.php';
$email = $_SESSION['email'];
$sql = "SELECT nome from usuarios where email_usuario ='$email'";
$stmt = $conexao -> prepare($sql);
$stmt -> execute();
$nome = $stmt -> fetch();
echo $nome;
?>

但我不知道哪种方法取得了选择的结果...... fetch()不起作用。

1 个答案:

答案 0 :(得分:0)

Fetch()返回一个数组。

尝试:

echo $nome['nome'];

echo $nome[0];

如果未指定,则默认情况下,您正在呼叫fetch(PDO::FETCH_BOTH)

从手册:

$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

print("PDO::FETCH_BOTH: ");
print("Return next row as an array indexed by both column name and number\n");
$result = $sth->fetch(PDO::FETCH_BOTH);
print_r($result);
print("\n");

输出:

PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number
Array
(
    [NAME] => banana
    [0] => banana
    [COLOUR] => yellow
    [1] => yellow
)