1. <legend id='Ou8Bv'><style id='Ou8Bv'><dir id='Ou8Bv'><q id='Ou8Bv'></q></dir></style></legend>

        <i id='Ou8Bv'><tr id='Ou8Bv'><dt id='Ou8Bv'><q id='Ou8Bv'><span id='Ou8Bv'><b id='Ou8Bv'><form id='Ou8Bv'><ins id='Ou8Bv'></ins><ul id='Ou8Bv'></ul><sub id='Ou8Bv'></sub></form><legend id='Ou8Bv'></legend><bdo id='Ou8Bv'><pre id='Ou8Bv'><center id='Ou8Bv'></center></pre></bdo></b><th id='Ou8Bv'></th></span></q></dt></tr></i><div id='Ou8Bv'><tfoot id='Ou8Bv'></tfoot><dl id='Ou8Bv'><fieldset id='Ou8Bv'></fieldset></dl></div>
          <bdo id='Ou8Bv'></bdo><ul id='Ou8Bv'></ul>
      2. <tfoot id='Ou8Bv'></tfoot>
      3. <small id='Ou8Bv'></small><noframes id='Ou8Bv'>

        PHP PDO 返回单行

        时间:2023-09-21

        <tfoot id='KVbBJ'></tfoot>
            <tbody id='KVbBJ'></tbody>

            <bdo id='KVbBJ'></bdo><ul id='KVbBJ'></ul>

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

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

                  <legend id='KVbBJ'><style id='KVbBJ'><dir id='KVbBJ'><q id='KVbBJ'></q></dir></style></legend>
                2. 本文介绍了PHP PDO 返回单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  更新 2:

                  那么这是可以得到的最优化的吗?

                  So is this the most optimized it can get?

                  $DBH = new PDO( "connection string goes here" );
                  
                  $STH = $DBH -> prepare( "select figure from table1" );
                  
                  $STH -> execute();
                  
                  $result = $STH -> fetch();
                  
                  echo $result ["figure"];
                  
                  $DBH = null;
                  

                  更新 1:

                  我知道我可以为 sql 查询添加限制,但我也想摆脱我不需要的 foreach 循环.

                  I know I can add limit to the sql query, but I also want to get rid of the foreach loop, which I should not need.

                  原始问题:

                  我有以下脚本,由于foreach"部分,它非常适合从数据库中返回许多行.

                  I have the following script which is good IMO for returning many rows from the database because of the "foreach" section.

                  如果我知道我总是只能从数据库中获取 1 行,我该如何优化它.如果我知道我只会从数据库中获取 1 行,我就不明白为什么需要 foreach 循环,但我不知道如何更改代码.

                  How do I optimize this, if I know I will always only get 1 row from the database. If I know I will only ever get 1 row from the database, I don't see why I need the foreach loop, but I don't know how to change the code.

                  $DBH = new PDO( "connection string goes here" );
                  
                  $STH = $DBH -> prepare( "select figure from table1" );
                  
                  $STH -> execute();
                  
                  $result = $STH -> fetchAll();
                  
                  foreach( $result as $row ) {
                      echo $row["figure"];
                  }
                  
                  $DBH = null;
                  

                  推荐答案

                  随便取.只得到一排.所以不需要 foreach 循环 :D

                  Just fetch. only gets one row. So no foreach loop needed :D

                  $row  = $STH -> fetch();
                  

                  示例(ty northkildonan):

                  example (ty northkildonan):

                  $id = 4;
                  $stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=? LIMIT 1"); 
                  $stmt->execute([$id]); 
                  $row = $stmt->fetch();
                  

                  这篇关于PHP PDO 返回单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:什么是 PDO &amp;我为什么要使用它? 下一篇:如何使用 PDO 清理输入?

                  相关文章

                  最新文章

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

                    1. <tfoot id='vKyp2'></tfoot><legend id='vKyp2'><style id='vKyp2'><dir id='vKyp2'><q id='vKyp2'></q></dir></style></legend>

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

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