php 如何使用 PDO 获取一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14035108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch a row with PDO
提问by Tom van der Woerdt
I'm trying not to get frustrated, but I've recently learned that mysql_*is deprecated in PHP. I've decided that I would learn how to use PDO.
我尽量不感到沮丧,但我最近了解到mysql_*PHP中已弃用。我决定学习如何使用 PDO。
I've just been looking at it this afternoon, and connecting to the database using it was easy, but then I wanted to fetch a row with it and save the row as an array indexed by the column names (the same way as the function mysql_fetch_arraydid). I can't figure it out to save my life.
今天下午我刚刚在看它,使用它连接到数据库很容易,但后来我想用它获取一行并将该行保存为由列名索引的数组(与函数相同的方式)mysql_fetch_array做过)。我无法想出它来挽救我的生命。
I'll post my code to clarify, and I'm sure it is something simple (all programming errors are always simple), but I am definitely doing something wrong.
我将发布我的代码以进行澄清,并且我确定它很简单(所有编程错误总是很简单),但我肯定做错了什么。
// Connect to the database
$host = $_PARAM["DatabaseServer"];
$db = $_PARAM["MainDatabase"];
$dbuser = $_PARAM["DatabaseUser"];
$dbpass = $_PARAM["DatabasePass"];
try
{
$Database = new PDO("mysql:host=$host;dbname=$db", $dbuser, $dbpass);
}
catch (PDOException $e)
{
echo "There was an unexpected error. Please try again, or contact us with concerns";
}
$stmt = $Database->prepare("SELECT * FROM users WHERE username=?");
$stmt->execute(array($sUserCook));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $row["username"];
回答by Tom van der Woerdt
fetchAlldoes what it says: it fetches all results for a query. Since it fetches a lot of results, you'll get an indexed array.
fetchAll做它所说的:它获取查询的所有结果。由于它获取了大量结果,因此您将获得一个索引数组。
fetchdoes what you might be looking for if you want only one row: it fetches one result for a query. If you're converting mysql_*code to PDO, the quickest way would be to just use this method instead of mysql_fetch_assoc.
fetch如果您只需要一行,则可以执行您可能正在寻找的操作:它为查询获取一个结果。如果您要将mysql_*代码转换为 PDO,最快的方法是使用此方法而不是mysql_fetch_assoc.
If you're still going with fetchAll: your code would probably just look like this:
如果您仍然使用fetchAll:您的代码可能看起来像这样:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
echo $row['username'];
}
Like I said, if you're just converting legacy code to PDO code, it might be easier to just change all queries to prepared statements and replace the following:
就像我说的,如果您只是将遗留代码转换为 PDO 代码,那么将所有查询更改为准备好的语句并替换以下内容可能会更容易:
mysql_fetch_assoc($result)
->$stmt->fetch(PDO::FETCH_ASSOC)mysql_num_rows($result)
->$stmt->rowCount()while ($row = mysql_fetch_assoc($result)) {}
->foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {}
mysql_fetch_assoc($result)
->$stmt->fetch(PDO::FETCH_ASSOC)mysql_num_rows($result)
->$stmt->rowCount()while ($row = mysql_fetch_assoc($result)) {}
->foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {}
回答by Kit
Use
用
$row = $stmt->fetch(PDO::FETCH_ASSOC);
You will get the first query result.
您将获得第一个查询结果。

