views:

2253

answers:

5

I have a table user_name with 3 fields, id, Name, Email (id is auto_increment field). I want to execute the following query in PHP, but its not returning any result.

INSERT INTO user_name (Name, Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';

When I am executing the above query in PHP as below then its not returning anything.

$_SQL="INSERT INTO user_name (Name,Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';";

$result_last_id = @mysql_query($_SQL);
$rs_insert = mysql_fetch_array($result_last_id);

$new_userid = $rs_insert['userid'];

Can anyone please tell me how to execute both queries into one.

+8  A: 

Give a look to the mysql_insert_id() function.

mysql_query($insertStatementOnly);
$new_userid = mysql_insert_id();
CMS
"Note: The value of the MySQL SQL function LAST_INSERT_ID() always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. " this is written in the link what you have given me. Still is it OK to use mysql_insert_id() ?
Prashant
Yes, just make sure to call it immediately after the insert query is executed.
CMS
Ok, thanks......
Prashant
This really is the best answer, and only way to do it.
lyrae
+1  A: 

Simple answer really: You just can't do it.

http://php.net/mysql_query

jezmck
+3  A: 

It appears you don't need to execute multiple queries, but I included how to do it below. What you want is the last inserted id, which you get from mysql_insert_id.

To execute multiple queries

From comments on documentation of mysql_query:

The documentation claims that "multiple queries are not supported".

However, multiple queries seem to be supported. You just have to pass flag 65536 as mysql_connect's 5 parameter (client_flags). This value is defined in /usr/include/mysql/mysql_com.h: #define CLIENT_MULTI_STATEMENTS (1UL << 16) /* Enable/disable multi-stmt support */

Executed with multiple queries at once, the mysql_query function will return a result only for the first query. The other queries will be executed as well, but you won't have a result for them.

Alternatively, have a look at the mysqli library, which has a multi_query method.

Blixt
+1  A: 

May I also suggest you avoid the error-suppression operator '@' in mysql_query as you may not be made aware of any mysql errors. At the very least do

mysql_query($sql) or die("error: " . mysql_error()) ;
Fanis
Very bad idea on production website. MySQL errors can contain some sensitive data. The best solution is to log em.
Maiku Mori
Definitely agree, which is why I said "at the very least". I didn't want to go off topic by expanding on how to handle database errors.
Fanis
+1  A: 

If you are using the Zend Framework with a PDO defined MySQL database, you would just use:

$database=Zend_Db::factory('PDO_MySQL',Array('hostname'=>'localhost','username'=>'x','password'=>'y','dbname'=>'z');
$connectionHandle=$database->getConnection();
$rowsInserted=$connectionHandle->insert('database_name','INSERT INTO x (a,b) VALUES (c,d)');
if ($rowsInserted>0) {
 $autoIncrementValue=$connectionHandle->lastInsertId();
}
Richy C.