2013-08-12 60 views
5

開始交易在mysql中是未定義的。我實際上使用它來在我的代碼中運行多個查詢來將一行從一個表移動到另一個表。很多幫助將不勝感激。好吧,我的問題是,爲什麼我的Begin_transaction()沒有定義?開始交易沒有定義

<?php 
    If(isset($trade_id)){ 
      $trade_id= $_GET['trade_id']; 
    } 
    require_once('connect.php'); 
    $mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database"); 
    try { 
     // First of all, let's begin a transaction 
     $mysqli->begin_transaction(); 

     // A set of queries; if one fails, an exception should be thrown 
     $mysqli->query("INSERT INTO `trade_history1` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close) 
     SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close 
     FROM `opentrades` 
     WHERE `trade_id` = " . $tradeid); 
     $mysqli->query("DELETE FROM `opentrades` WHERE `trade_id` = " . $trade_id); 

     // If we arrive here, it means that no exception was thrown 
     // i.e. no query has failed, and we can commit the transaction 
     $mysqli->commit(); 
     $_SESSION['message'] = 'Successfully deleted'; 
    } catch (Exception $e) { 
     // An exception has been thrown 
     // We must rollback the transaction 
     $_SESSION['message'] = 'Unable to delete'; 
     $mysqli->rollback(); 
    } 
    $mysqli->close(); 

      // if we successfully delete this, we 
      if ($successfullyDeleted) { 
       $_SESSION['message'] = 'Successfully deleted'; 
      } else { 
       $_SESSION['message'] = 'Unable to delete'; 
      } 

      header('Location: js.php'); 

    ?> 
+0

你可以在需要更清晰? – rags

+0

您忘記提問了。 – zerkms

回答

4

$mysqli->begin_transaction();必須$mysqli->autocommit(FALSE);

入住這

http://www.php.net/manual/en/mysqli.commit.php

+0

這使錯誤消失,但刪除沒有發生,可能是什麼問題? –

+0

爲此,您需要檢查刪除查詢 –

+0

好吧它設法讀取刪除查詢,但它沒有運行插入查詢。 –

-2

您可以使用此代碼我已經測試。

面向對象的風格

$cid->multi_query('start transaction;'); 
$cid->multi_query("insert into demo values('1','Pankaj','9031251290')"); 
$cid->multi_query('commit;'); 

程序風格

mysqli_query($link, "start transaction;"); 
mysqli_query($link, "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)"); 
mysqli_query($link, "commit;"); 
+0

爲什麼'multi_query'? –

+0

實際上,我一次執行多個查詢,所以我使用這個。 – pankaj