执行数据库查询后关闭会话 [英] Close session after executing database query

查看:156
本文介绍了执行数据库查询后关闭会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对joomla和sql的经验很少,非常感谢您的帮助! 我正在使用joomla 2.5,正在从数据库中查询数据,并使用以下代码将其存储在内存中:

I have very little experience with joomla and sql and I would really appreciate your help! I am using joomla 2.5 and I am querying data from the database and storing it in memory with the following code:

function getList()
{
    $mainframe = JFactory::getApplication('site');
        $db = JFactory::getDBO();
        $query = "  SELECT 
                    * 
                FROM 
                    #__ListUser 
                WHERE
                    $db->setQuery( $query );"
        $rows = $db->loadObjectList();
        return $rows;
}

我有3个问题,

  1. 查询数据库时,将打开一个新的数据库会话,是否需要在它关闭后还是自动将其关闭?
  2. 您是否知道实现此方法的更有效方法(用户会话内存大小约为11MB!)
  3. 使用此方法访问数据库是否存在安全问题?
  1. When I query the database, a new DB session is opened, Do I need to close it after or is automatic?
  2. Do you know of a more efficient way to achieve this method (a user session memory size is about 11MB!)
  3. Is there any security issue with accessing the database using this method?

非常感谢!任何帮助将不胜感激!

Thank you very much! any help would be very appreciated!

推荐答案

代码应如下所示(我现在不知道它如何工作):

The code should look like this (I don't see how it can work now):

function getList()
{
   // $mainframe = JFactory::getApplication('site'); // you don't need this line!
        $db = JFactory::getDBO();
        $query = "  SELECT 
                    * 
                FROM 
                    #__ListUser 
                WHERE
                    1=1"; // just some condition to extract selected rows
        $db->setQuery( $query ); // this sets the query and it's joomla, not sql.
        $rows = $db->loadObjectList();
        return $rows;
}

请注意WHERE ....需要一个条件(否则,如果要所有行,请删除WHERE及其后的内容)

Please note the WHERE .... needs a condition (else if you want all the rows, remove WHERE and what follows)

  1. 您不需要关闭它
  2. 11Mb不一定是由于该查询造成的,请尝试添加LIMIT 0,1(仅返回一行),您会发现内存变化不大.在全局配置中打开调试,然后重新加载组件.在页面的最底部,您将看到哪些扩展正在占用您的内存.尽管大多数安装都可以接受11Mb.
  3. 如果要使用输入参数创建WHERE条件,只需确保$ db-> quote()任何值即可防止SQL注入.

这篇关于执行数据库查询后关闭会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆