::におけるlastInsertId - におけるlastInsertIdや店舗におけるlastInsertId前にコミットトランザクションのリターンを使用した場合http://php.net/manual/en/pdo.lastinsertid.php
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$stmt = $dbh->prepare("INSERT INTO test (name, email) VALUES(?,?)");
try {
$dbh->beginTransaction();
$tmt->execute(array('user', '[email protected]'));
print $dbh->lastInsertId();
$dbh->commit();
} catch(PDOExecption $e) {
$dbh->rollback();
print "Error!: " . $e->getMessage() . "</br>";
}
} catch(PDOExecption $e) {
print "Error!: " . $e->getMessage() . "</br>";
}
?>
だけ覚えておいてください。
ストアドプロシージャの場合 - LAST_INSERT_ID()を使用します。
BEGIN
INSERT INTO test (name, email) VALUES ('value1', 'value2');
SET out_param = LAST_INSERT_ID();
END
EDIT 1:out_paramに問題に直面し、その結果として、最後のインサートIDを返すことを選択し使用している場合http://php.net/manual/en/mysqli.insert-id.php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = mysqli->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();
printf ("New Record has id %d.\n", $stmt->insert_id);
/* close connection */
$mysqli->close();
から
あなたがMySQLiをを使用している場合 - その後、mysqli_insert_idを使用しています。
BEGIN
DECLARE last_id INT DEFAULT 0;
INSERT INTO test (name, email) VALUES ('value1', 'value2');
SET last_id = LAST_INSERT_ID();
SELECT last_id;
END
EDIT 2:
あなたはストアドプロシージャを取得するには、問題に直面している場合はコード次利用を結果セット -
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf("-----------------\n");
}
} while ($mysqli->next_result());
}
コードをfollwingアウトのparam使用にアクセスするには -
// execute the stored Procedure
// @uid - IN param, @userCount - OUT param
$result = $connect->query('call IsUserPresent(@uid, @userCount)');
// getting the value of the OUT parameter
$r = $connect->query('SELECT @userCount as userCount');
$row = $r->fetch_assoc();
$toRet = ($row['userCount'] != 0);
私はPDOではなくMySQLiで作業しています。 –
答えを編集しました。それが助けてくれることを願って。 – DarkKnight
編集をありがとう。私がしたことは、部分的にあなたが提案したものでした( "$ result"と "$ r"を使用して、値を取得するより直接的な方法で)。 、?、?、?、?);););););););); $ stmt1 - > bind_param( "siiii"、$ a、$ s、$ d、$ f、$ g); \t $ stmt1 - > execute(); \t $ stmt1 - > close() ; \t $ stmt2 = $ conn->( "LAST_INSERT_ID()を選択")を調製; \t $ stmt2 - >実行(); \t $ stmt2 - > bind_result($ QWE); \t $ stmt2 - >フェッチ( ); \t $ stmt2 - > close(); ' –