首页 \ 问答 \ PHP / MYSQL查询作为多用途函数(PHP/MYSQL Query as a Function for Multi-Use)

PHP / MYSQL查询作为多用途函数(PHP/MYSQL Query as a Function for Multi-Use)

我试图在网站的大约15页内清理大量的选择查询,我想使用一个函数,然后我可以使用它来调用所有不同的查询和表,将减少一堆杂乱。

我遇到了一个问题,似乎无法找到一种方法来纠正它。

这是功能:

function dbGet($conn, $table, $select, $where, $cond) {
   require($conn);
   if ( $stmt = $db->prepare(" SELECT $select FROM `" . $table . "` WHERE $where=? ")) {
      $stmt->bind_param("i", $cond);
      $stmt->execute();
      $result = $stmt->get_result();
      $rowNum = $result->num_rows;

      if ( $rowNum > 0 ) {
         while( $data = $result->fetch_assoc() ){
            return $data;
         }
      }
      $stmt->close();
   } else {
      die($db->error);
   }
   $db->close();
}

以下是我在各种不同页面中调用该函数的方法:

$settings = dbGet('php/connect.php', 'settings', '*', 'userId', $sessionId);
echo $settings->toName;

我遇到的问题是,它只显示设置表中的第一条记录。 我以为在函数中使用while循环会返回所有记录。 事实上,如果我为$ data而不是return返回print_r,我会获得所有记录。 如何将所有记录传递到要在任何其他页面中访问的返回$数据?

或者如果你们碰巧对现有功能的任何建议都是高度重视和真实的,我可以实施而不是自己编写,请告诉我。

谢谢。


I am trying to clean a ton of select queries inside about 15 pages of a website and I thought using a function that then I can use to call all the different queries and tables, would reduce the clutter a bunch.

I am running into a problem and can't seem to figure out a way to get it right.

Here is the function:

function dbGet($conn, $table, $select, $where, $cond) {
   require($conn);
   if ( $stmt = $db->prepare(" SELECT $select FROM `" . $table . "` WHERE $where=? ")) {
      $stmt->bind_param("i", $cond);
      $stmt->execute();
      $result = $stmt->get_result();
      $rowNum = $result->num_rows;

      if ( $rowNum > 0 ) {
         while( $data = $result->fetch_assoc() ){
            return $data;
         }
      }
      $stmt->close();
   } else {
      die($db->error);
   }
   $db->close();
}

And here is how I call the function in the various different pages:

$settings = dbGet('php/connect.php', 'settings', '*', 'userId', $sessionId);
echo $settings->toName;

The problem I am running into, is that it is only displaying the first record from the settings table. I thought using the while loop in the function would return all the records. As a matter of fact, if I do a print_r for $data instead of return, I get all the records. How can I pass along all the records into the return $data to be access in any other page?

OR If you guys happen to have any suggestions of an existing function that is highly regarded and true-and-tested that I can implement instead of writing my own, please let me know.

Thanks.


原文:https://stackoverflow.com/questions/45282796
更新时间:2021-12-06 22:12

最满意答案

必须使用散列表或其他机制来实现集合对象,这些机制平均提供对集合中元素数量的次线性访问时间。

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects


Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects

相关问答

更多
  • 它是线性的,即需要O(n)时间,其中n是列表A中元素的数量,正如您可以从官方Python文档中推断出来的那样 。 It's linear, that is it takes O(n) time, where n is the number of elements in the list A, as you can infer from the official Python documentation.
  • 你的编辑是在正确的轨道上。 我们可以通过n(输入数量)和m(表格大小)得到长度的表达式吗? 对于1,如果哈希表大小以某种方式被禁止,这意味着负载因子(即每桶的项目数)n / m大于1并且不是常数也不在恒定范围内,那么可以假设关系m = f (n),则负载因子将是n / f(n),因此复杂度也将是O(n / f(n))。 在第二种情况下,复杂性总是O(1)。 Your edit is on the right track. Can we get an expression for the length in ...
  • 答案似乎是从其他功能的文档中暗示的: https : //developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message SpaceUsed()明显慢于ByteSize() ,因为它是使用反射实现的(而不是生成的ByteSize()代码实现)。 与ByteSize()一样 ,它的CPU时间与为proto定义的字段数是线性的。 和 ByteSizeLong()通常是为proto定义的字段数量的线性。 我实际上并 ...
  • 必须使用散列表或其他机制来实现集合对象,这些机制平均提供对集合中元素数量的次线性访问时间。 http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the ...
  • 一组n个项目中大小为k的不同子集的数量是n!/(k!*(nk)!) 。 这也是存储值的地图的最佳存储复杂性。 您可以使用二叉树林来表示子集。 节点将包含一个键和两个子指针。 叶节点将表示大小为1的子集。叶节点上方级别上的父节点将表示大小为3的子集,因为它们包含一个键并指向包含键的两个叶节点。 下一级父节点将表示大小为7的子集,依此类推。 您将获得一个具有n!/(k!*(nk)!)可能的根节点的林。 您的“简单地图”将是从根节点映射到存储值的哈希映射。 要获得最佳存储复杂性,您需要避免重复等效节点。 在林中创 ...
  • 它在Javadoc中被指定为O(1)。 因此算法的复杂性是O(N)。 但即使没有 containsKey()调用,实际上也是不必要的。 你所要做的就是测试put()是否返回一个非空值,它表示重复。 当我们使用containsKey时,我们必须为每个键计算一个散列函数,然后与我们的搜索项进行比较,对吧? 错误。 我们计算搜索关键字的散列值并检查该存储桶是否被相等的密钥占用。 那么复杂性是不是O(n)? 没有。 It is specified in the Javadoc as O(1). The comple ...
  • 是的,O(N 2 )是正确的。 编辑:就“从第一原则”而言, 有点难以猜测他们可能想要什么,但我猜他们正在寻找(实质上)某种证据(或至少是指示)的东西)满足big-O的基本定义: 存在正常数c和n 0 ,使得: 对于所有n≥n0,0≤f(n)≤cg(n)。 因此,找到16N 2 + 3N后的下一步是找到n 0和c的正确值。 至少乍一看,c看起来是16,并且n 0 ,-3,(可能被视为0,负数的元素没有实际意义)。 Yes, O(N2) is correct. Edit: It's a little hard ...
  • Manager.prototype = Object.create(Employee.prototype); 是实现与Java相似结果的JavaScript方式 Manager extends Employee 没有这条线, Manager就不会像其他Employee行事。 它说,“如果你不能找到一个经理会做什么,那么他会做同样的事情,员工会做”。 默认情况下,所有Java类都从Object继承; 同样,所有JavaScript类都共享一个默认原型。 就像你不需要说Employee extends Ob ...
  • _unordered_set_被实现为哈希表 ,也就是说, 哈希表的一个常见实现是使用哈希桶的容器(例如:like vector) (它是同一个unordered_set元素的容器(例如:like list))桶)。 在unordered_set中插入元素时,会应用散列函数,然后为您提供放置的存储区。 可能会有多个元素插入到同一个存储桶中,当您找到一个元素时,哈希函数会应用,为您提供存储桶,您需要搜索他们正在寻找的元素。 最糟糕的情况是所有元素都在同一个桶中结束(取决于用于存储同一个桶O(n)中的元素的容器 ...
  • S的基数为3^(N(N-1)/2)因为每对可以有三个状态( 00 ),并且对的数量是矩阵中的条目数(NxN) ,减去对角线(N)上的条目数除以2 (每对2个条目)。 The cardinality of S is 3^(N(N-1)/2) since each pair can have three states (00,01,10), and the number of pairs is the number of entries in the matrix (NxN), minus the number ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)