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

        <legend id='IWy8H'><style id='IWy8H'><dir id='IWy8H'><q id='IWy8H'></q></dir></style></legend>

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

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

        如何制作 PDO 类方法,用于在 arg 中使用未知数量

        时间:2023-10-04

              <tbody id='bPd0s'></tbody>
              <bdo id='bPd0s'></bdo><ul id='bPd0s'></ul>
              <legend id='bPd0s'><style id='bPd0s'><dir id='bPd0s'><q id='bPd0s'></q></dir></style></legend>
              <tfoot id='bPd0s'></tfoot>

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

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

                • 本文介绍了如何制作 PDO 类方法,用于在 arg 中使用未知数量的参数插入/更新/删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  目前我正在尝试创建一个 PDO 类,我将在其中使用一种方法来运行诸如 INSERT、UPDATE 或 DELETE 之类的查询.

                  Currently Im trying to create a PDO class where I will have a method to run a query like INSERT, UPDATE, or DELETE.

                  例如,这是我的 SELECT 方法

                  For examaple this is my method for a SELECT

                   public function getPreparedQuery($sql){
                      $stmt = $this->dbc->prepare($sql);
                      $stmt->execute([5]);
                      $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
                      if(!$arr) exit('No rows');
                      $stmt = null;
                      return $arr;
                  }
                  

                  我只是这样称呼它:

                      $stmt = $database->getPreparedQuery($sql2);
                  var_export($stmt);
                  

                  到目前为止,我知道 runQuery 应该像这样工作:

                  And so far I know that a runQuery should work something similar to this:

                  不使用方法插入示例:

                  $idRol = "6";
                  $nomRol = "test6";                          
                  $stmt = $database->dbc->prepare("insert into roles (idRol, NomRol) values (?, ?)");
                  $stmt->execute(array($idRol,$nomRol));
                  $stmt = null;
                  

                  但我想把它变成一个通用的方法,我可以简单地传递sql语句,就像这样:

                  But I want to make it into an universal method where i simply can pass the sql sentence, something like this:

                  $database->runQuery($query);
                  

                  但查询可能恰好是

                  $query = "插入角色 (idRol, NomRol) VALUES ('4','test')";

                  $query = "INSERT INTO roles (idRol, NomRol) VALUES ('4','test')";

                  $query = "插入电影 (movName, movLength, movArg) VALUES ('name1','15','movie about...')";

                  $query = "INSERT INTO movies (movName, movLength, movArg) VALUES ('name1','15','movie about...')";

                  那么我如何对 arg $query 进行切片,以便我可以获取所有正在使用的变量以创建一个通用的 runQuery?

                  So how do I slice the arg $query so I can get all the variables that are being used in order to make an universal runQuery?

                  因为我可以想象我的方法应该是这样的

                  Because I can imagine that my method should be something like this

                  runQuery([$var1 , $var2, $var3....(there can be more)] , [$var1value, $var2value, $var3value...(there can be more)]){
                  
                          $stmt = $database->dbc->prepare("insert into movies($var1 , $var2, $var3....(there can be more)) values (?, ? , ? (those are the values and there ca be more than 3)"); //how do i get those "?" value and amount of them, and the name of the table fields [movName, movLength, movArg can be variable depending of the sentence]?
                          $stmt->execute(array($var1,$var2, $var3, ...)); //this is wrong , i dont know how to execute it
                          $stmt = null;
                  }
                  

                  推荐答案

                  您需要向函数中添加第二个参数.只是一个数组,所有这些变量都会在其中.根据定义,数组可以包含任意数量的元素,这正好可以解决您的问题:

                  You need to add a second parameter to your function. Simply an array where all those variables would go. An array by definition can have an arbitrary number of elements, which solves your problem exactly:

                  public function runQuery($sql, $parameters = []) {
                      $stmt = $this->dbc->prepare($sql);
                      $stmt->execute($parameters);
                      return $stmt;
                  }
                  

                  这个简单的函数将运行任何查询.您可以在我专门介绍 PDO 辅助函数的文章中查看使用示例:

                  this simple function will run ANY query. You can see the usage example in my article dedicated to PDO helper functions:

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

                  如您所见,现在您的函数可以用于具有任意数量参数的任何查询

                  As you can see, now your function can be used with any query with any number of parameters

                  这篇关于如何制作 PDO 类方法,用于在 arg 中使用未知数量的参数插入/更新/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:php sql 将来自不同数据库的多个表连接在一起 下一篇:带有 html 内容的 PHP PDO bindParam

                  相关文章

                  最新文章

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

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

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

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