Current location: Hot Scripts Forums » Programming Languages » PHP » Trouble with PDO / Transactional operation

Trouble with PDO / Transactional operation

Reply
  #1 (permalink)  
Old
Icepick.pt's Avatar
Newbie Coder
 
Join Date: Nov 2010
Location: Setúbal, near Lisbon
Posts: 96
Thanks: 3
Thanked 7 Times in 7 Posts
Trouble with PDO / Transactional operation

Hi guys.
I have been using PDO to handle a MySQL database, and also have been trying to do it with transactions.
However, in the script below I am not getting the transaction enforced:
- the SQL queries are ok. If I dont mess them up, everything goes fine and both statements are written to the database.
- if I mess any of the SQL statments, I would expect that none of them would be written to the database. The exception does get thrown, and I do get the echoed error message on the screen but the correct SQL statement does not get rolled back as it is supposed to be and is still written.

Can any of you let me know what am I doing wrong here?
PHP Code:

    

    
try {  
        
// habilita as excepções
        
$dbwrite->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
// inicia a transacção
        
$dbwrite->beginTransaction();
        
        
// realiza a primeira operação de escrita 
        
$stringUpdate="
            update tabela1
            set campo1=        :conteudo1_,
                campo2=        :conteudo2_,
                campo3=        :conteudo3_
            where umaCondicaoQualquer
        "
;
        
        
$pedido=NULL;
        
$pedido=$dbwrite->prepare($stringUpdate);
        
// limpa as variáveis que vai escrever na bd
        
$pedido->bindParam(':conteudo1_'$_POST['post1'], PDO::PARAM_INT);
        
$pedido->bindParam(':conteudo2_'$_POST['post2'], PDO::PARAM_INT);
        
$pedido->bindParam(':conteudo3_'$_POST['post3'], PDO::PARAM_STR);
        
$pedido->execute();
                
        
// realiza a segunda operação de escrita 
        
$stringUpdate="
            update tabela2
            set campoA=        :conteudoA_,
                campoB=        :conteudoB_,
                campoC=        :conteudoC_
            where outraCondicaoQualquer
        "
;
        
        
$pedido=NULL;
        
$pedido=$dbwrite->prepare($stringUpdate);
        
// limpa as variáveis que vai escrever na bd
        
$pedido->bindParam(':conteudoA_'$_POST['postA'], PDO::PARAM_INT);
        
$pedido->bindParam(':conteudoB_'$_POST['postB'], PDO::PARAM_INT);
        
$pedido->bindParam(':conteudoC_'$_POST['postC'], PDO::PARAM_STR);
        
$pedido->execute();
        
        
// finaliza a escrita das duas operações
        
$dbwrite->commit();
        }
    
    
// se ocorreu uma excepção
    
catch (Exception $e) {
        
// desfaz toda a operação
        
$dbwrite->rollBack();
        echo 
$e;
        } 
__________________
You can allways find me at www.datagen.eu, I own it.
And if you are hiring, I 'm all ears, now.
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
Icepick.pt's Avatar
Newbie Coder
 
Join Date: Nov 2010
Location: Setúbal, near Lisbon
Posts: 96
Thanks: 3
Thanked 7 Times in 7 Posts
Solved!

One of the tables was MyISAM and that engine does not suport transactions. I changed it to InnoDB and that did the trick.
__________________
You can allways find me at www.datagen.eu, I own it.
And if you are hiring, I 'm all ears, now.

Last edited by Icepick.pt; 02-21-13 at 04:22 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 09:33 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.