2012-01-25 97 views
8

我有一個「事件」表。爲了簡單起見,您可以想象它應該像分層類別。它使用嵌套集模型(感謝馬克Hillyer他在http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/後)在codeigniter中執行多個不能逐個執行的查詢

我的代碼:

$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:

我無法執行$查詢$這 - > DB->查詢($查詢);;

解決方案1,沒有工作:

I.使用的mysqli分貝引擎。

二, 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


如何糾正此代碼的工作?我很樂意爲第一個問題尋找解決方案。

+0

你可以有一個查詢的數組,並在循環 – GordonM

+0

沒有執行它們,你可以在這種情況下,看到執行作爲單獨的查詢不起作用,因爲它不記得'@ myRight'和'@ Level'從過去查詢的結果變量 –

+0

直到斷開連接後變量才被保留嗎? – GordonM

回答

12

也許使用交易?

$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(); 

http://ellislab.com/codeigniter/user-guide/database/transactions.html

+0

謝謝。我現在不處於測試你的答案的狀況。無論如何,因爲它似乎是正確的,無憂無慮的,我接受它,直到我真正檢查它是如何工作的。 –

2

爲什麼不寫一個存儲過程來執行上面列出的所有SQL,將查詢中的變量作爲參數。然後,只需將該存儲過程作爲單個SQL語句來調用;

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

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(); 
} 
+0

我會補充說,你必須確保'system/application/config/database.php'中的dbdriver行被設置爲'mysqli': '$ db ['default'] ['dbdriver'] =「mysqli 「;'這篇文章正是我需要加快我的批量插入。非常感謝你。 –

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); 
+0

你可以請格式化查詢嗎? – WhatsThePoint