Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting multiple queries in codeigniter that cannot be executed one by one
    primarykey
    data
    text
    <p>I have an "event" table. For simplicity, you can imagine that it should be like a <strong>hierarchical category</strong>. It uses the <strong>nested set model</strong> (Thanks to Mark Hillyer for his post at <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="noreferrer">http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/</a>)</p> <p>My code:</p> <pre><code>$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 &gt; @ParentRight; UPDATE event SET lft = lft + 2 WHERE lft &gt; @ParentRight; INSERT INTO event(lft, rgt, level) VALUES(@ParentRight, @ParentRight + 1, @Level); UNLOCK TABLES;"; mysqli_multi_query($this-&gt;db-&gt;conn_id, $query); $data['id'] = $this-&gt;db-&gt;insert_id(); return $this-&gt;db-&gt;update('event', $data); </code></pre> <p>And after that I'm going to update the last inserted object with <code>$this-&gt;db-&gt;update('event', $data)</code></p> <p><code>$data</code> is an array that user has filled.</p> <hr> <p>Problem 1:</p> <p>I couldn't execute <strong>$query</strong> with <strong>$this->db->query($query);</strong>;</p> <p>Solution 1 that didn't work:</p> <p>I. Use <strong>mysqli</strong> db engine.</p> <p>II. <code>mysqli_multi_query($this-&gt;db-&gt;conn_id, $query);</code> While I thought it works, it is giving the following error:</p> <p><code>Commands out of sync; you can’t run this command now.</code></p> <hr> <p>Problem 2:</p> <p><code>$this-&gt;db-&gt;insert_id()</code> doesn't work (returns 0)</p> <p>Problem 3:</p> <p><code>$this-&gt;db-&gt;update('event', $data);</code> errors: <code>Commands out of sync; you can't run this command now</code></p> <hr> <p>How can I correct this code to work? I'd be even happy to find a solution for the first problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload