在codeigniter中执行多个查询,这些查询无法逐个执行

时间:2012-01-25 08:56:21

标签: php mysql codeigniter mysqli multi-query

我有一个“事件”表。为简单起见,您可以想象它应该像层次结构类别。它使用嵌套集模型(感谢Mark Hillyer在http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/发帖)

我的代码:

$query = 
"LOCK TABLE event WRITE;
SELECT @ParentRight := parent.rgt, @Level := parent.level FROM event AS parent WHERE parent.id = '{$data['parent_id']}';

UPDATE event SET rgt = rgt + 2 WHERE rgt > @ParentRight;
UPDATE event SET lft = lft + 2 WHERE lft > @ParentRight;

INSERT INTO event(lft, rgt, level) VALUES(@ParentRight, @ParentRight + 1, @Level);
UNLOCK TABLES;";

mysqli_multi_query($this->db->conn_id, $query);

$data['id'] = $this->db->insert_id();
return $this->db->update('event', $data);

之后我将用$this->db->update('event', $data)

更新最后插入的对象

$data是用户填写的数组。


问题1:

我无法使用 $ this-> db->查询($ query)执行 $ query ; ;

无效的解决方案1:

予。使用 mysqli 数据库引擎。

II。 mysqli_multi_query($this->db->conn_id, $query);虽然我认为它有效,但却出现以下错误:

Commands out of sync; you can’t run this command now.


问题2:

$this->db->insert_id()不起作用(返回0)

问题3:

$this->db->update('event', $data);错误: Commands out of sync; you can't run this command now


如何更正此代码?我很乐意为第一个问题找到解决方案。

5 个答案:

答案 0 :(得分:14)

也许使用交易?

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');
$this->db->trans_complete(); 

https://www.codeigniter.com/user_guide/database/transactions.html

答案 1 :(得分:3)

为什么不编写一个存储过程来执行上面列出的所有SQL,将查询中的变量作为参数。然后只需将存储过程作为单个SQL语句调用;

$sql = "CALL some_sp($param1, $param2...etc)";

答案 2 :(得分:3)

MySQL中的用户定义变量是特定于连接的,并且一直保持到关闭连接为止。

$queryList = array(
    "LOCK TABLE event WRITE",
    "SELECT @ParentRight := parent.rgt, @Level := parent.level FROM event AS parent WHERE parent.id = '{$data['parent_id']}'",
    "UPDATE event SET rgt = rgt + 2 WHERE rgt > @ParentRight",
    "UPDATE event SET lft = lft + 2 WHERE lft > @ParentRight",
    "INSERT INTO event(lft, rgt, level) VALUES(@ParentRight, @ParentRight + 1, @Level)",
    false, // special value
    "UNLOCK TABLES",
) 

foreach ($queryList as $query) if ($query) {
    mysqli_query($this->db->conn_id, $query);
    // you should have error-checking here
} else {
    // we have the special value
    $data['id'] = $this->db->insert_id();
}

答案 3 :(得分:0)

$query1 = $this->db->query("SELECT * FROM `Wo_Products` WHERE `boost_plan`=1 AND `tamatar`=1 AND`active`=1 ORDER BY rand()  LIMIT 3");

$query2 = $this->db->query("SELECT * FROM `Wo_Products` WHERE `boost_plan`=2 AND `tamatar`=1 AND`active`=1 ORDER BY rand()  LIMIT 3");

$query3 = $this->db->query("SELECT * FROM `Wo_Products` WHERE `boost_plan` NOT IN (0,1) AND `tamatar`=1 AND`active`=1 ORDER BY rand()  LIMIT 6");

    $result1 = $query1->result();
    $result2 = $query2->result();
    $result3 = $query3->result();
    return array_merge($result1, $result2, $result3); 

答案 4 :(得分:-2)

您可以简单地使用以下内容:

 print_r($this->db->query("YOUR query NO 1") ? 'Success Q1' : $this->db->error());
 print_r($this->db->query("YOUR query NO 2") ? 'Success Q2' : $this->db->error());
 print_r($this->db->query("YOUR query NO 3") ? 'Success Q3' : $this->db->error());