2012-03-17 1 views
8

私が注文テーブルに順序を挿入するために準備されたステートメントを使用していたが、今私が注文テーブルに挿入された最後のIDに基づいて注文項目のテーブルに注文項目を挿入する:mysql_insert_id()に相当するものは何ですか?準備文を使用して?

if (isset($_POST['process'])) { 

    $order_name = $_POST['order_name']; 
    //create initial order 
    $stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)"); 
    $stmt->bind_param('s', $order_name); 
    $stmt->execute(); 

    //this gets the most recent auto incremented ID from the database - this is the order_id we have just created 
    $order_id = mysql_insert_id(); 

    //loop over all of our order items and add to the database 
    foreach ($_SESSION['order'] as $item) { 

同等である何準備された文を使用してmysql_insert_id();に?

+0

は、あなたの質問は何ですか? –

+0

mysql_insert_id()とは何ですか?準備されたステートメントスタイルで? – user1227124

+1

つまり、PDOライブラリのmysql_insert_idに相当するものは何ですか? [ドキュメント](http://www.php.net/manual/en/pdo.lastinsertid.php)に何か問題がありますか? –

答えて

17

PDOを使用している場合は、PDO::lastInsertId()です。だからあなたのデータベースハンドルが$link呼び出された場合:あなたはMySQLiをを使用している場合

$order_id = $link->lastInsertId(); 

、それはmysqli::$insert_idまたはmysqli_insert_id()です:あなたが挿入され、IDを取得するには$stmt->insert_idを試すことができます

$order_id = $link->insert_id; 
+0

しかし、私は準備された文を使って、PDOでないmysqli – user1227124

+0

@ user1227124:それは[mysqli :: $ insert_id'](http://php.net/manual/en/mysqli.insert-id.php)または 'mysqli_insert_id( ) '。 – Ryan

+0

ありがとうございました! – user1227124

関連する問題