简体   繁体   English

如何“分解”我的PDO查询返回的数组?

[英]How do I 'break down' the array returned by my PDO query?

So just as I began to get a good grasp on MySQL, I found out I should be learning about PDO. 就在我开始掌握MySQL的时候,我发现我应该学习PDO。 This is the (unfinished) code I have so far. 这是我到目前为止的(未完成的)代码。 I'd like to get it working before worrying about adding more password security. 在担心增加更多密码安全性之前,我想让它工作。 When I run the script, the printed message is "Array". 当我运行脚本时,打印的消息是“Array”。 How can I break this down and retrieve the data in the array? 如何解决这个问题并检索数组中的数据?

try {
$db = new PDO($dbhost, $dbuser, $dbpassword);
$statement = $db->prepare("select * from users where email = :name");
$statement->execute(array(':name' => $username));
$result = $statement->fetch();
echo $result;}

catch(PDOException $e) {echo $e->getMessage();}

Sorry if this is a silly question; 对不起,如果这是一个愚蠢的问题; I've been searching for a while, but can not find any good examples what match what I'm doing, and I learn better from examples than long, wordy descriptions (such as the PHP manual). 我一直在寻找一段时间,但找不到任何与我正在做的事情相匹配的好例子,而且我从例子中学到的东西比长而冗长的描述(例如PHP手册)更好。

You might want to review your PHP base first, these pages seems most appropriate: 您可能希望首先查看您的PHP基础,这些页面似乎最合适:

http://php.net/manual/en/language.types.array.php http://php.net/manual/en/language.types.array.php

Then, you can review again the code for Fetch and FetchAll at: 然后,您可以再次查看Fetch和FetchAll的代码:

http://php.net/manual/en/pdostatement.fetch.php http://php.net/manual/en/pdostatement.fetch.php

Note that arrays are the defacto standard when receiving data from any provider, should it be pdo, mysql, mysqli, pgsql, this is why i recommend you review the ARRAY type first, get a good grasp of it, then read PDO stuff again... 注意,当从任何提供者接收数据时,数组是事实上的标准,如果它是pdo,mysql,mysqli,pgsql,这就是为什么我建议你首先查看ARRAY类型,好好掌握它,然后再次阅读PDO的东西。 ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM