I created a simple MySQLi class for a project I'm working on to make it easier and more streamlined to pull from and push to the DB. (also to get more familiar with OOP in PHP)

我为一个正在开发的项目创建了一个简单的MySQLi类,以便更轻松,更简化,从而推送到数据库。 (也是为了更熟悉PHP中的OOP)

A problem I keep running into is in an effort to make it as efficient as possible. I try to close / free every query / statement / result set. In doing so I get the following error a lot:

我一直遇到的一个问题是努力使其尽可能高效。我尝试关闭/释放每个查询/语句/结果集。这样做我得到了以下错误:

Warning: mysqli_result::close(): Couldn't fetch mysqli_result

I think I get the above specifically because it's trying to close the query twice in a row.

我想我特意得到了上述内容,因为它试图连续两次关闭查询。

Currently my class can make prepared and unprepared statements. So I try to close queries / statements in 2 places. I check when preparing a statement if I have already prepared a statement, if so I need to close the old one before I make a new one and lastly in the destructor of the class.

目前我的班级可以做出准备和毫无准备的陈述。所以我尝试在两个地方关闭查询/语句。如果我已经准备好了一个语句,我会在准备语句时进行检查,如果是这样的话,我需要在创建一个语句之前关闭旧语句,最后在类的析构函数中。

I realize I could just close the query / statement after I've pulled and stored the result but this seems to be messy for a few reasons. For one it removes the possibility of reusing the prepared statement. As well as disallowing me to pull some information about the query after it's been run such as affected rows and the like.

我知道我可以在拉取并存储结果之后关闭查询/语句,但由于某些原因,这似乎很麻烦。一方面,它消除了重复使用准备好的陈述的可能性。同时禁止我在运行后提取有关查询的一些信息,例如受影响的行等。

I know I could just store this info for every query and what not but it just seems like the proper way to go about this problem would be to close / free the query / statement if I need to make another one, and again at the end of the script.

我知道我可以只为每个查询存储此信息,但是只是看起来正确的解决这个问题的方法是关闭/释放查询/语句,如果我需要创建另一个查询/语句,并在最后再次的脚本。

I have tried looking around and reading up on how I should properly handle this task but I have been unable to come up with anything.

我试着环顾四周,阅读我应该如何正确处理这项任务,但我一直无法想出任何东西。

So my question is, is there anyway to test if a query or statement needs to be closed or freed? Or is there a better way I should be attempting to tackle this issue?

所以我的问题是,无论如何要测试查询或语句是否需要关闭或释放?或者,我是否有更好的方法来解决这个问题?

Thanks for any help or insight anyone can offer.

感谢任何人提供的任何帮助或见解。

5 个解决方案

#1


1

I think that if there are no rows left in the result set, it should be closed automatically. So, if you iterate through the whole result set, there's no need to call mysqli_result. However, you should close it if you fetch, let's say, only the first row from a result set containing about 100 rows.

我认为如果结果集中没有剩余行,则应自动关闭。因此,如果遍历整个结果集,则无需调用mysqli_result。但是,如果您只提取包含大约100行的结果集中的第一行,则应该关闭它。

更多相关文章

  1. PHP实现字符串转换成查询语句
  2. Mysql数据库四大特性、事物的四个隔离、基本MySQL语句、独立表空
  3. 为什么准备好的语句由每个会话管理?
  4. MYSQL必知必会-SQL语句查询
  5. MySQL-数据库安装及基本SQL语句
  6. 8.2.1.2-MySQL如何优化 WHERE 语句
  7. ORACLE 分页SQL语句
  8. MYSQL查询语句:排名在第3名的成绩是多少?
  9. MYSQL中update语句 与in 的bug

随机推荐

  1. Android----Google code android开源项目
  2. Android 新浪微博代码
  3. Java.lang.ClassNotFound Instrumentatio
  4. android 判断service是否开启
  5. android 命令大全
  6. How to install Android SDK and play wi
  7. Android横竖屏切换时,不重新onCreate()的
  8. Android Studio V0.2.x For Windows下载
  9. android短信发送
  10. Hostapd Android