你要在插入记录之后,调用 last_insert_id() 才会有结果!
mysql> desc article;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      |      | PRI | NULL    | auto_increment |
| title   | varchar(255) |      |     |         |                |
| content | text         |      |     |         |                |
| author  | varchar(20)  |      |     |         |                |
| email   | varchar(30)  |      |     |         |                |
| date    | varchar(40)  |      |     |         |                |
+---------+--------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)mysql> desc article;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      |      | PRI | NULL    | auto_increment |
| title   | varchar(255) |      |     |         |                |
| content | text         |      |     |         |                |
| author  | varchar(20)  |      |     |         |                |
| email   | varchar(30)  |      |     |         |                |
| date    | varchar(40)  |      |     |         |                |
+---------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)mysql> insert into article(title) values('hava a test!!!');
Query OK, 1 row affected (0.16 sec)mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.03 sec)mysql> insert into article(title) values('hava a test!!!');
Query OK, 1 row affected (0.00 sec)mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)