1

I have the following SQL that works directly in MySQL

 INSERT INTO `my_tabel` (`data`) VALUES ("my_value");
 SELECT * from `my_tabel` ORDER BY `id` DESC LIMIT 1

It inserts a row, and then gets the new row so that I can return the new ID to use later.

When I try to run the SQL in codeIgniter I get an error message stating that I have an error in my SQL

$m = new my_model();
$sql = 'INSERT INTO `my_tabel` (`data`) VALUES ("'.$my_value.'"); SELECT * from `my_tabel` ORDER BY `id` DESC LIMIT 1';
$m->query($sql);

Running a single SQL statement works fine in codeIgniter, but not when I add the second SELECT... statement.

Any ideas (or alternative solutions)?

Thanks

3
  • Don't put values in queries like that, it's very unsafe. Do something like this: $m->query('INSERT INTO `my_tabel` (`data`) VALUES (?); SELECT * from `my_tabel` ORDER BY `id` DESC LIMIT 1', array($my_value));. More info at the bottom of this page: codeigniter.com/user_guide/database/queries.html Commented May 17, 2012 at 16:13
  • in php I'm adding mysql_real_escape_string($value), I didn't add it to the question to keep the code simpler. I can see your solution adds security, so I will add that too Commented May 17, 2012 at 16:20
  • Thing is, if you use the question marks, you don't need to use mysql_real_escape_string anymore. Commented May 17, 2012 at 16:37

7 Answers 7

2

This is not a limitation of CI but that of the database client libraries.

only a single query can be executed at a time.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 PHP only allows a query to run 1 command at a time, in a single string. If one wants to run multiple queries, you'll need to use the multi_query method. But since you're using CI, thats not recommended.
0

Why not run two $m->query() statements? That way you can check the success of each individually from CI.

In most SQL interfaces, having multiple statements in a query is problematic because of the different types of result from each. Even when calling a stored procedure, it is necessary to carefully craft the stored procedure not to return extraneous results, though even that can usually be handled with a special, relaxed api which allows multiple results.

3 Comments

Originally that was how I set it up, but I get the same error message. Running them both in separate functions works, but not when they are called at the same time.
@Eruant: Huh? How can you call separate functions at the same time?
Sorry to confuse. If I run the insert command on it's own it works. If I then edit the function to only run the select command, that works too. The problem happens when a write them both into the same function.
0

insert_id() is your friend!

$this->db->insert_id()

From here http://codeigniter.com/user_guide/database/helpers.html

So something like:

$insert_data = array(
  'data' => $my_value
);

$this->db->insert('my_table', $insert_data);

$last_id = $this->db->insert_id();

Comments

0

Instead of running 2 queries, to get the id created after the insert query use:

$this->db->insert_id();

Comments

0

As noted in the other answers, use $this->db->insert_id() is better because there is a chance that another user inserts a row between your two queries. Since PHP can only execute one query at a time, this can become a race condition.

Comments

0

Thanks for everyone's help. The solution that worked for me was

$m = my_model();
$m->data = $value;
$m->save();
echo $m->id;

Comments

0

http://www.codeigniter.com/userguide2/database/active_record.html#caching You can use Caching to control multiple queries without conflicts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.