方法概要
* MySQL の関数 LAST_INSERT_ID() を利用
サンプル
$name = $_POST["name"];
$birthday = $_POST["birthday"];
$remarks = $_POST["remarks"];
$connectionId = mysql_connect("XXXXX:1000", "dataBaseName", "passowrd") or die(mysql_error());
mysql_select_db('dataBaseName') or die(mysql_error());
// データ新規登録
$sql = "INSERT INTO person (name, birthday, remarks) VALUES ("
. $name . "', '"
. $birthday . "', '"
. $remarks . "')";
$sql = "SELECT LAST_INSERT_ID()";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$lastId = $row [0];
echo "Last Id : " . $lastId;
参考資料
http://muchag.undo.jp/archives/65
方法概要
* Zend framework の関数 lastInsertId() を利用
サンプル
$name = $_POST["name"];
$birthday = $_POST["birthday"];
$remarks = $_POST["remarks"];
// データ新規登録
require_once 'Zend/Db.php';
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini('./configs/db_info.ini', 'hello_db');
$db = Zend_Db::factory($config->db);
$sql = "INSERT INTO person VALUES(?, ?, ?)";
$statement = new Zend_Db_Statement_Pdo($db, $sql);
$result = $statement->execute(array($name, $birthday, $remarks));
$lastInsertId = $db->lastInsertId();
echo "Last Id : " . $lastId;
参考資料
http://framework.zend.com/issues/browse/ZF-3837