• <tfoot id='YjUmY'></tfoot><legend id='YjUmY'><style id='YjUmY'><dir id='YjUmY'><q id='YjUmY'></q></dir></style></legend>
    1. <small id='YjUmY'></small><noframes id='YjUmY'>

        <bdo id='YjUmY'></bdo><ul id='YjUmY'></ul>
      <i id='YjUmY'><tr id='YjUmY'><dt id='YjUmY'><q id='YjUmY'><span id='YjUmY'><b id='YjUmY'><form id='YjUmY'><ins id='YjUmY'></ins><ul id='YjUmY'></ul><sub id='YjUmY'></sub></form><legend id='YjUmY'></legend><bdo id='YjUmY'><pre id='YjUmY'><center id='YjUmY'></center></pre></bdo></b><th id='YjUmY'></th></span></q></dt></tr></i><div id='YjUmY'><tfoot id='YjUmY'></tfoot><dl id='YjUmY'><fieldset id='YjUmY'></fieldset></dl></div>

        如何使用 PDO 获取一行

        时间:2023-10-05
          <tbody id='k7isJ'></tbody>
        <legend id='k7isJ'><style id='k7isJ'><dir id='k7isJ'><q id='k7isJ'></q></dir></style></legend>

        <i id='k7isJ'><tr id='k7isJ'><dt id='k7isJ'><q id='k7isJ'><span id='k7isJ'><b id='k7isJ'><form id='k7isJ'><ins id='k7isJ'></ins><ul id='k7isJ'></ul><sub id='k7isJ'></sub></form><legend id='k7isJ'></legend><bdo id='k7isJ'><pre id='k7isJ'><center id='k7isJ'></center></pre></bdo></b><th id='k7isJ'></th></span></q></dt></tr></i><div id='k7isJ'><tfoot id='k7isJ'></tfoot><dl id='k7isJ'><fieldset id='k7isJ'></fieldset></dl></div>
          <bdo id='k7isJ'></bdo><ul id='k7isJ'></ul>

                • <small id='k7isJ'></small><noframes id='k7isJ'>

                • <tfoot id='k7isJ'></tfoot>
                • 本文介绍了如何使用 PDO 获取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我尽量不感到沮丧,但我最近了解到 mysql_* 在 PHP 中已被弃用.我决定学习如何使用 PDO.

                  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_fetch_array 所做的那样).我想不出它来挽救我的生命.

                  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_array did). I can't figure it out to save my life.

                  我会发布我的代码来澄清,我确信它很简单(所有编程错误总是很简单),但我肯定做错了什么.

                  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"];
                  

                  推荐答案

                  fetchAll 做它所说的:它获取查询的所有结果.由于它获取了大量结果,因此您将获得一个索引数组.

                  fetchAll does what it says: it fetches all results for a query. Since it fetches a lot of results, you'll get an indexed array.

                  fetch 会执行您可能正在寻找的操作:它为查询获取一个结果.如果您要将 mysql_* 代码转换为 PDO,最快的方法是使用此方法而不是 mysql_fetch_assoc.

                  fetch does 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.

                  如果您仍然使用 fetchAll:您的代码可能看起来像这样:

                  If you're still going with fetchAll: your code would probably just look like this:

                  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
                  foreach ($rows as $row) {
                      echo $row['username'];
                  }
                  

                  就像我说的,如果您只是将遗留代码转换为 PDO 代码,将所有查询更改为准备好的语句并替换以下内容可能会更容易:

                  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:

                  • 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) {}

                  这篇关于如何使用 PDO 获取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 PHP 中创建到 PDO 的连接时出错 下一篇:默认情况下,PDO 是否总是使用模拟准备好的语句

                  相关文章

                  最新文章

                  <i id='mSPx6'><tr id='mSPx6'><dt id='mSPx6'><q id='mSPx6'><span id='mSPx6'><b id='mSPx6'><form id='mSPx6'><ins id='mSPx6'></ins><ul id='mSPx6'></ul><sub id='mSPx6'></sub></form><legend id='mSPx6'></legend><bdo id='mSPx6'><pre id='mSPx6'><center id='mSPx6'></center></pre></bdo></b><th id='mSPx6'></th></span></q></dt></tr></i><div id='mSPx6'><tfoot id='mSPx6'></tfoot><dl id='mSPx6'><fieldset id='mSPx6'></fieldset></dl></div>
                  <tfoot id='mSPx6'></tfoot>
                  <legend id='mSPx6'><style id='mSPx6'><dir id='mSPx6'><q id='mSPx6'></q></dir></style></legend>

                  <small id='mSPx6'></small><noframes id='mSPx6'>

                      • <bdo id='mSPx6'></bdo><ul id='mSPx6'></ul>