1. <tfoot id='zAZKS'></tfoot>

        <bdo id='zAZKS'></bdo><ul id='zAZKS'></ul>
    2. <small id='zAZKS'></small><noframes id='zAZKS'>

    3. <legend id='zAZKS'><style id='zAZKS'><dir id='zAZKS'><q id='zAZKS'></q></dir></style></legend>
      1. <i id='zAZKS'><tr id='zAZKS'><dt id='zAZKS'><q id='zAZKS'><span id='zAZKS'><b id='zAZKS'><form id='zAZKS'><ins id='zAZKS'></ins><ul id='zAZKS'></ul><sub id='zAZKS'></sub></form><legend id='zAZKS'></legend><bdo id='zAZKS'><pre id='zAZKS'><center id='zAZKS'></center></pre></bdo></b><th id='zAZKS'></th></span></q></dt></tr></i><div id='zAZKS'><tfoot id='zAZKS'></tfoot><dl id='zAZKS'><fieldset id='zAZKS'></fieldset></dl></div>
      2. 如何在php pdo中使用where子句多次调用函数?

        时间:2023-09-20
        • <small id='cOkwn'></small><noframes id='cOkwn'>

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

          <legend id='cOkwn'><style id='cOkwn'><dir id='cOkwn'><q id='cOkwn'></q></dir></style></legend>
              <bdo id='cOkwn'></bdo><ul id='cOkwn'></ul>

                <tbody id='cOkwn'></tbody>

                  <tfoot id='cOkwn'></tfoot>

                  本文介绍了如何在php pdo中使用where子句多次调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是 php 的新手,正在努力学习它为什么你们和女孩需要原谅我问了很多!

                  I am new to php and trying hard to learn its why you guys and gals need to Forgive me for asking a lot!

                  这是我的问题;

                  我试图多次调用带有 where 子句的函数,我已经阅读了几乎所有的帖子和示例,但仍然不明白该怎么做.

                  I am trying to call a function with where clause multiple times, I have read allmost all posts and examples still didn't understand how to do it.

                  我认为一个例子比我能写的任何简介都更有用.

                  I tought that An example will be more useful than any blurb I can write.

                  这是我尝试创建并多次使用的函数:

                  Here is the function I am trying to create and use it multiple times :

                  function getTable($tableName, $clause) {
                          $stmt = $pdo->prepare("SELECT * FROM ".$tableName." WHERE ".$clause." = :".$clause);
                          $stmt->bindParam(":$clause", $clause, PDO::PARAM_STR);
                          $stmt->execute();
                          if($stmt->rowCount() > 0){
                              return true;
                          }else{
                              return false;
                          }
                      return $stmt;
                  }
                  

                  我不确定我的功能是否安全或正确.

                  I am not sure if my fucntion is safe or its rigth.

                  这就是我试图调用函数的方式,我不知道如何调用表名和 where 子句以及如何打开 while 循环.

                  AND this is how I am trying to call function, which I dont know how to call table name and where clause and how to turn while loop.

                  getTable('posts');
                  

                  如果你能给出一个创建和调用函数的例子,我将不胜感激,谢谢

                  If you give an example of creating and caling function, I would be grateful, Thanks

                  推荐答案

                  不,你的函数不安全.此外,它只是无用的.没有任何用例可以像这样使用它 getTable('posts');.对于其他一切,最好允许完整的 SQL 语法,而不是某些有限的子集.

                  Nope, your function is not safe. Moreover it is just useless. There is no use case where you would use it like this getTable('posts');. And for the everything else it is much better to allow the full SQL syntax, not some limited subset.

                  我能想到的最简单但最强大的 PDO 函数是一个接受 PDO 对象、SQL 查询和带有输入变量的数组的函数.返回 PDO 语句.我在关于 PDO 辅助函数的文章中写到了这样的函数.所以这是代码:

                  The simplest yet most powerful PDO function I can think of is a function that accepts a PDO object, an SQL query, and array with input variables. A PDO statement is returned. I wrote about such function in my article about PDO helper functions. So here is the code:

                  function pdo($pdo, $sql, $args = NULL)
                  {
                      if (!$args)
                      {
                           return $pdo->query($sql);
                      }
                      $stmt = $pdo->prepare($sql);
                      $stmt->execute($args);
                      return $stmt;
                  } 
                  

                  使用此功能,您将能够使用任意数量的 WHERE 条件运行任何查询,并获得多种不同格式的结果.以下是上述文章中的一些示例:

                  With this function you will be able to run any query, with any number of WHERE conditions, and get results in many different formats. Here are some examples from the article mentioned above:

                  // getting the number of rows in the table
                  $count = pdo($pdo, "SELECT count(*) FROM users")->fetchColumn();
                  
                  // the user data based on email
                  $user = pdo($pdo, "SELECT * FROM users WHERE email=?", [$email])->fetch();
                  
                  // getting many rows from the table
                  $data = pdo($pdo, "SELECT * FROM users WHERE salary > ?", [$salary])->fetchAll();
                  
                  // getting the number of affected rows from DELETE/UPDATE/INSERT
                  $deleted = pdo($pdo, "DELETE FROM users WHERE id=?", [$id])->rowCount();
                  
                  // insert
                  pdo($pdo, "INSERT INTO users VALUES (null, ?,?,?)", [$name, $email, $password]);
                  
                  // named placeholders are also welcome though I find them a bit too verbose
                  pdo($pdo, "UPDATE users SET name=:name WHERE id=:id", ['id'=>$id, 'name'=>$name]);
                  
                  // using a sophisticated fetch mode, indexing the returned array by id
                  $indexed = pdo($pdo, "SELECT id, name FROM users")->fetchAll(PDO::FETCH_KEY_PAIR);
                  

                  特别适合你,这里是 while 的例子,虽然这个方法被认为是笨拙和过时的:

                  Special for you, here is the while example, though this method is considered clumsy and outdated:

                  $stmt = pdo($pdo,"SELECT * FROM tableName WHERE field = ?",[$value]);
                  while ($row = $stmt->fetch()) {
                      echo $row['name'];
                  }
                  

                  这篇关于如何在php pdo中使用where子句多次调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在另一个while循环内的while循环内执行mysqli准备好 下一篇:如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取

                  相关文章

                  最新文章

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

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

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