Вопрос

I have a PHP function which inserts multiple records into MySQL:

function commit_purchase($asset_type_ID, $org_ID, $asset_desc, $asset_cost, $date, $org_to_member_ID, $asset_ID, $purchaser_cur_invest, $purchaser_cred_deb, $purchaser_balance) {
    global $db;
    $query = "START TRANSACTION;
          INSERT INTO assets
            (asset_type_ID, org_ID, asset_desc, asset_cost, asset_value, purchase_date, is_approved)
          VALUES
            (:asset_type_ID, :org_ID, :asset_desc, :asset_cost, :asset_cost, :date, 1);
          SET @asset_ID = LAST_INSERT_ID();
          INSERT INTO cash_out
            (org_to_member_ID, amount, description, date, is_approved, asset_ID)
          VALUES
            (:org_to_member_ID, :asset_cost, :asset_desc, :date, 1, @asset_ID);
          SET @cash_out_ID = LAST_INSERT_ID();
          INSERT INTO shares
            (asset_ID, member_ID, percent_owner, is_approved)
          SELECT assets.asset_ID, pending_asset_shares.member_ID, pending_asset_shares.percent_owner, pending_asset_shares.is_approved
          FROM assets, pending_asset_shares
          WHERE assets.asset_ID = @asset_ID;
          DELETE FROM pending_asset_shares
          WHERE asset_ID = :asset_ID;
          DELETE FROM pending_assets
          WHERE pending_asset_ID = :asset_ID;
          INSERT INTO trans_log
             (translog_id, trans_type, org_to_member_ID, date, purchaser, asset_ID, cur_invest, cash_out_ID, cred_deb, balance)
          VALUES
             (DEFAULT, 3, :org_to_member_ID, :date, :org_to_member_ID, @asset_ID, :purchaser_cur_invest, @cash_out_ID, :purchaser_cred_deb, :purchaser_balance);
          COMMIT;";
$statement = $db->prepare($query);
$statement->bindValue(':asset_type_ID', $asset_type_ID);
$statement->bindValue(':org_ID', $org_ID);
$statement->bindValue(':asset_desc', $asset_desc);
$statement->bindValue(':asset_cost', $asset_cost);
$statement->bindValue(':date', $date);
$statement->bindValue(':org_to_member_ID', $org_to_member_ID);
$statement->bindValue(':purchaser_cur_invest', $purchaser_cur_invest);
$statement->bindValue(':purchaser_cred_deb', $purchaser_cred_deb);
$statement->bindValue(':purchaser_balance', $purchaser_balance);
$statement->bindValue(':asset_ID', $asset_ID);
$statement->execute();
$statement->closeCursor();
return $asset_ID;

I am trying to use the first INSERT statment's LAST_INSERT_ID (@asset) as a variable for my next function. The way I am calling the above function, in hopes of setting the variable, is:

$asset_ID = commit_purchase($asset_type_ID, $org_ID,.......etc.)

I am pretty sure my problem is somewhere around the "return $asset_ID" in my SQL statement. I have been able to do this successfully when using only 1 LAST_INSERT_ID call.

Nothing is being returned at all.

Это было полезно?

Решение

Ok, as mentioned in my comments, you can use beginTransaction to break this up. http://php.net/manual/en/pdo.begintransaction.php

Once you have done that, it's just a matter of getting the last inserted ID. You can use lastInsertId for that: http://php.net/manual/en/pdo.lastinsertid.php

Другие советы

Breaking this down into multiple queries would really be the best solution, but to answer your original question: If you want to get the value of MySQL variables in PHP, just execute a SELECT query:

$asset_ID = mysql_result( mysql_query( 'SELECT @asset_ID' ) );
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top