tags:

views:

397

answers:

7

I have the following code:

  public function createNewGuide($userID,$guideName)
  {
    $sql =" INSERT INTO myTable(name, updated) 
            VALUES ('$guideName', 'NOW()')";

    //Process query
    $this->query($sql); // This inserts the new row
    $this->query('LAST_INSERT_ID()'); // This throws an error

    return $this->query_result;
  }

My query function looks like this:

  private function query($sql) 
  {
      $this->query_result = mysql_query($sql, $this->conn)
        or die("Unable to query local database <b>". mysql_error()."</b><br>$sql");   
  } 

I get the following error:

MySQL Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LAST_INSERT_ID()'

I've googled and looked at similar problems, but not found an answer :(

I have not tried the PHP function mysql_insert_id(), as I really would like to do this using SQL.

+1  A: 

As you are using the mysql_* functions, why not just use the mysql_insert_id function, instead of calling LAST_INSERT_ID() yourself ?


Still, the SQL error you are getting is probably because the SQL query you are sending to the server is this one :

LAST_INSERT_ID()

Instead of this one :

select LAST_INSERT_ID()

There should be a select, if you are doing an SQL query to... select... some data.

Pascal MARTIN
yes, you are correct. I'm not using the PHP function, because I'm not sure if it will return the correct ID if multiple users at the same time ads or updates data in the same table. However, using `select LAST_INSERT_ID()` only returns 0....
Steven
I'm pretty sure `mysql_last_id` will do exactly what you want, as it's working on the current connection. And it's what I've always used, actually ^^
Pascal MARTIN
+2  A: 

You forgot SELECT:

"SELECT LAST_INSERT_ID()"
Mark Byers
Thanks, that was it. But it returns 0 :( Why is that?
Steven
+5  A: 

Why not just use PHP's mysql_insert_id?

Irrespective...

SELECT LAST_INSERT_ID()

...should work as long as you've an auto-increment column in the table.

middaparka
Because I think it's safer to get the correct ID using the SQL statement? E.g. when multiple users inserts and updates data in the same table.
Steven
Unless you're using InnoDB and carrying out the above within a transaction, I don't think this will prove to be the case. That said, I believe that the mysql_insert_id is connection specific, hence what you're referring to won't be an issue unless you're sharing the same database connection.
middaparka
A am using InnobDB. My problem now with `LAST_INSERT_ID()`, is that is just returns 0. Have no idea why.
Steven
Just checking the basics, but does your table actually have an auto-increment column?
middaparka
yes. The rows are created with unique ID's.
Steven
ok, I ended up using mysql_insert_id() - since I can't figure out why `LAST_INSERT_ID()` only returns 0.
Steven
+1  A: 

That won't work without a SELECT:

SELECT LAST_INSERT_ID();

or just use mysql_insert_id, it's a php function which does the same on the php level. However, use the first method if your table ids are BIGINT.

Psaniko
+1 for the BIGINT tip
Charlie Somerville
A: 
SELECT LAST_INSERT_ID();

If I were you. I would get your insert/select last_insert_id to work from the command line or query browser first, before php. At minimum, this will at least confirm or deny correct sql syntax.

Kris Krause
A: 

The guys have already answered that you were missing the SELECT prefix.

By the way, you should watch your INSERT statement... it has a clear door for SQL injection if $guideName is not escaped.

Clash
A: 

LAST_INSERT_ID() returns zero if no row was inserted.

You should check that your INSERT actually succeeded. Always test the return value of mysql_query() and other functions, which is usually FALSE if an error occurred.

$sql =" INSERT INTO myTable(name, updated) 
        VALUES ('$guideName', 'NOW()')";

if ($this->query($sql) === FALSE) {
  die(mysql_error());
}

if (($result = $this->query("SELECT LAST_INSERT_ID()")) === FALSE) {
  die(mysql_error()); 
}

if (($row = mysql_fetch_array($result)) === FALSE) {
  die(mysql_error()); 
}

$id = $row[0];
Bill Karwin