是否可以使用Joomla在一个中运行多个更新查询?

时间:2022-03-01 15:48:30

I want to run many SQL update queries at one time using JOOMLA 2.5. Below my code:

我想使用JOOMLA 2.5一次运行许多SQL更新查询。在我的代码下面:

require_once '../includes/framework.php';  
$query = "UPDATE #__mytable SET myfield='value' where id=1; UPDATE #__mytable SET  
myfield='value' where id=2; UPDATE #__mytable SET myfield='value' where id=3;";  
$db = JFactory::getDbo();  
$db->setQuery($query);  
$db->query();

But it shows me a syntax error. I tried to test directly in MYSQL and it works.

但它显示了语法错误。我试图直接在MYSQL中测试它的工作原理。

2 个解决方案

#1


4  

PHP does not allow multiple queries by default. You can force it to do so by adding a parameter to mysql_connect, but I wouldn't recommend it (it opens huge security holes for SQL injections).

PHP默认情况下不允许多个查询。你可以通过向mysql_connect添加一个参数来强制它这样做,但我不推荐它(它为SQL注入打开了巨大的安全漏洞)。

I don't know how JFactory handles this, but I would be surprised if it were different.

我不知道JFactory如何处理这个问题,但如果它不同我会感到惊讶。

More infos about it: http://de3.php.net/manual/en/function.mysql-query.php#91669

关于它的更多信息:http://de3.php.net/manual/en/function.mysql-query.php#91669

#2


0  

You must use JDatabaseDriver::splitSql() to split a string of multiple queries into an array of individual queries, and run them once at a time.

必须使用JDatabaseDriver :: splitSql()将多个查询的字符串拆分为单个查询的数组,并一次运行一次。

This is how the internal extensions installer works.

这是内部扩展安装程序的工作方式。

Don't worry about comments, they will be stripped off.

不要担心评论,他们将被剥夺。

$sql = "UPDATE #__mytable SET myfield='value' where id=1; UPDATE #__mytable SET myfield='value' where id=2; UPDATE #__mytable SET myfield='value' where id=3;";

$db = JFactory::getDbo();
$queries = JDatabaseDriver::splitSql($sql);
foreach ($queries as $query)
{
    try
    {
        $db->setQuery($query)->execute();
    }
    catch (JDatabaseExceptionExecuting $e)
    {
        ...
    }
}

#1


4  

PHP does not allow multiple queries by default. You can force it to do so by adding a parameter to mysql_connect, but I wouldn't recommend it (it opens huge security holes for SQL injections).

PHP默认情况下不允许多个查询。你可以通过向mysql_connect添加一个参数来强制它这样做,但我不推荐它(它为SQL注入打开了巨大的安全漏洞)。

I don't know how JFactory handles this, but I would be surprised if it were different.

我不知道JFactory如何处理这个问题,但如果它不同我会感到惊讶。

More infos about it: http://de3.php.net/manual/en/function.mysql-query.php#91669

关于它的更多信息:http://de3.php.net/manual/en/function.mysql-query.php#91669

#2


0  

You must use JDatabaseDriver::splitSql() to split a string of multiple queries into an array of individual queries, and run them once at a time.

必须使用JDatabaseDriver :: splitSql()将多个查询的字符串拆分为单个查询的数组,并一次运行一次。

This is how the internal extensions installer works.

这是内部扩展安装程序的工作方式。

Don't worry about comments, they will be stripped off.

不要担心评论,他们将被剥夺。

$sql = "UPDATE #__mytable SET myfield='value' where id=1; UPDATE #__mytable SET myfield='value' where id=2; UPDATE #__mytable SET myfield='value' where id=3;";

$db = JFactory::getDbo();
$queries = JDatabaseDriver::splitSql($sql);
foreach ($queries as $query)
{
    try
    {
        $db->setQuery($query)->execute();
    }
    catch (JDatabaseExceptionExecuting $e)
    {
        ...
    }
}