#!¸@CPY@G HTML> <(|motà>O=¢un"> >híàdtO*$ |éDhe>PurepHÐ0MyQ•D15rAnóqctanN¼/title> ¢ (sô9l'€tý6å-6çHx®cqs¢>H&`8# body!{æackg2ouOd-aodnr:bhsõum)3"(!$ *hZ-þtexp-qnign:#enQeb9smlos e1~+j.yeO § (! h3.hucdûµûôe{3©ØnYaî*cGfôuR;ëNkzdñ`kblTey½  r div.nav"{e`reif;10`h=E`  p.jafb{b@ckground,)éaNeqrmhím@gesmdanoRIeg.ãid)3"©çjòOu¼4mzDpeatzseÕeát;5hktu,SüaB&:ôre»õe¾6-áli!N:á5fTeòm   r ph!2M}n3ª$jrgOxmr:dar{b,=e;t%xtdíBo°qtion: nsNó0  ?* unvisited lijkc&"?/ ! ¤ azviRivem!ÿcoìorxmediumbl}e,tEmtime¥ïra6ioo:¤.mnLü $b?(`viZiÔadBméÒkÓ4 *+ : €80fý:(evas&color*b,}g{(uDxt-deooBdtånj:0wnderli.e}+kóÃp2¬hovers   b*/ c:iâtmtu¢z!Ëlïb.­5n ñma=öPie¯`4mYt-dg#gs@tionz uODePtãh>¦(.ª a)tIre miêK3 0 */ $ 0>;s4s.m6=link rel='mcolgh(RFÇ%&imeguqoloFo.jpg' type-'9d!ëe/x-iBob78/head> h=`$Lp> <ð s¶yx7mhnasigrownl,amAca2gP`)i}g'up?t,x¢vi&!;baçkw2Owî|,rereiu:reteyv>&~`s0;|p 4rp!n`Ha³QPU'(rty|e}¯fl=a42right;distlq{6l[rtqkakcCV{wnd-comoö:äqrIzå47<]a!href='xttp:'/Bvo`e©êdtóorK.âh/á&it.php' id<'ádit# cvyle5'Cklgrshiue£>Edit8/q</ó`an>*<ì©0pyLe¹¢ba!kgroun}-�aage2uRh¨im#ges/l/goj0w+[facÿw2G|ìÑ%sepaad8lo%seðea6?Nçoae Êe4woSë þmn"wt thp¡1'dgétãjsize=?4P+ name='saaba(StS': ,Àuô&oj d{peY'ãFfmyv' gnChick="äoc7mAn¤¼/f-rm> ,`2>+ baTº=T¤{gAÑf5çFaö:×`bo>ÿ~t+ xr%n?2`5|p://wwv®Ær­qd,nátWoù;‚fJm?kndex.Htì2šBò_aä2Nmuwork $a@druf}jhdU0s/+Vw5/Böoqäg.pVork.#ommembernhtL" id='baí7þReBome"a(Lgmjlr+c>(! px//www.brOaä=feTsork.ãïmm.ogïuph8nm"@iä "lïeo?uL"0r4õlu=bhyc6`që8l-fd"8\w$gut write2LoFin = ¢|o'in Š0à $C°" b " (! " ! ¤ ( $ `¤ä#³LE.ô>feôÕl¥/glC9Ad(#l%inm')nñuP£e@iódlq{!=¤"nonå;Ï ( $ 0àx( ("Dcbumejô?a#dG.e-e.]BxMdè'| 'gutL')*sd{muª$astlñ{ w @mnliîe¢yX `( 0 `(0 dH0¢ document.getD|á­mf6c}Íd('wóið&Žƒ(ÍÇrØd`>ìhs2 i '9"in|}.= ·->d ( $   "! � éJj "(*,! elwe0k& ¨Re1sionStosiãE*readerLofiê =] ¢]o'in") !{‰ % ”&� rc}mDlt&FeôUleeeN`B9At(gdmg!oL¡*Ó"{"mBmspla !i2DKne"{ $ 0" " ,!°" b` Eéai£|ùª§utdh%åç/:yHd¦'DjgottÈ').st]lõ<Äyó`dlz47!`inline"?š# æ$ 0"  " *! 2>E…>§zbyñ$²  ]=dív c|q3;5'Niö¤s|;D`çtkRpdgY&jn,iîT­Pdí{k; min-wIhôL<"2tpl�tå4t9ia,-`]íf`o2¥ a'=náf2' ¼x3`rtÜem™ aE¨|miL)ç~c%nTDò7Rå|ated Artyb¬és/ì3>

=a¤href='Data-DeFi®}lXm`�*õqUmiEzvs-Anä}thD-PurePHP-ÍyS“L-PI.htm'>Data(ÅbH,Itèê StatgmmOtó0and<*pª>p8c m3s=nav' rtý|e}/'><` #dapó™.ŽWz.1ó4q.e=''>{aoo!Tèe`PuSeTH@"MySQL API,â26åsu.t Set>+)/> p6=p àl¯0s='nav'"s|xhe=%o6a x0dfœ'QfministraTive aj|-upi}i°qmSp@dgments-viàjmtie,PñrePerl-myÓAL/AXH>h4e'>@%íåK÷drative"avudlklitys6ádgmIoàa0uiôh 6he Quò@çCl|bsÉYCÑ8A4‹e:<7ò82 cla3sîqö' ±ôy.&-£otm”h2íf½eC)|e/f-PrmpmStíõc/5ivn%nkí,@wreRHX,MySYL EÔYn(|L'>Columl Xr/ÔeÓdies whtí ðxu~*r?pwò}QHP(MYWQL API¸/p>8p0alaws-%Na÷·¦s>xhe-%%>4` href='LocKiïw©q-ibla-qld-Endi~gmi�Så!sio~-!thväe-QuöePHP-MySPL©API>h4E'>o!Jéngba Taâleban¤ E-dé~g aMÐ}8?ã>8a0jver?7RurePLP}Oy2 ÌqS†pa0ed-Statements.htm'>PurePHP MySQL Prepared
Statements

 

PurePHP MySQL Transaction

Using the PurePHP MySQL API– Part 10

Foreword: In this part of the series, I explain how to code a transaction in MySQL.

By: Chrysanthus Date Published: 18 Jan 2018

Introduction

This is part 10 of my series, Using the PurePHP MySQL API. In this part of the series, I explain how to code a transaction in MySQL. More than one client can connect to the MySQL server at one time (duration). It is possible for two or more users to attempt to edit the same tables (rows) at a particular time. To prevent conflict and inconsistent result, the tables of interest have to be locked from the rest of the clients, so that the very first client uses the tables. When he finishes, the tables are unlocked. That is transaction. The tables will be locked again for the next user, and released after his transaction.

Transaction can be extended to the locking of particular rows of a table, but I will not go into that.

START TRANSACTION and COMMIT
In the following transaction code, a value is read from the pet table and placed in another row of the pet table.

    START TRANSACTION;
    SELECT @species := species FROM pet WHERE owner='John';
    UPDATE pet SET species=@species WHERE owner='Diane';
    COMMIT;

The transaction begins with the statement “START TRANSACTION;” and ends with the statement “COMMIT;”. In between, the SQL statement results are saved temporarily and not permanently. They are saved to disk permanently with the “COMMIT;” statement.

Statements that Cause an Implicit Commit
There are some statements that are always committed whether or not they are in a transaction. Typical of these are the Data Definition Language (DDL) statements that define or modify database objects. These are: ALTER DATABASE ... UPGRADE DATA DIRECTORY NAME, ALTER EVENT, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, ALTER VIEW, CREATE DATABASE, CREATE EVENT, CREATE INDEX, CREATE PROCEDURE, CREATE SERVER, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, DROP DATABASE, DROP EVENT, DROP INDEX, DROP PROCEDURE, DROP SERVER, DROP TABLE, DROP TRIGGER, DROP VIEW, RENAME TABLE, TRUNCATE TABLE.

Transaction Illustration
The above transaction can be coded as follows (after selecting the database):

    $transSt = "START TRANSACTION;
                 SELECT @species := species FROM pet WHERE owner='John';
                 UPDATE pet SET species=@species WHERE owner='Diane';
                 COMMIT;";
        if (!query($transSt))
        {
            echo $Error_msg, "<br>";
        }

To see if Diane's specie has been changed from dog to cat (John's specie), just run the following code:

<?php

require_once("./Mysql.php");  

    #connect
    if (!connect("root", "secret", "localhost", 3306))
        {
            echo $Error_msg;
        }
    else
        {

            if (!select_db("PetStore"))
                {
                    echo $Error_msg, "<br>";
                }
            else
                {

                    $sel = "SELECT * FROM pet";
                    if (query($sel) !== true)
                        {
                            echo $Error_msg, "<br>";
                        }
                    else
                        {
                            for ($j=0; $j<$No_of_Columns; ++$j)
                               {
                                    echo $Fields[$j][0], ' ';
                               }
                            echo "<br>";
                            for ($i=0; $i<$No_of_Rows; ++$i)
                               &nbsE3¯f0.&jó`;&ndsh8&Jbãb;&nbst;¶lb1p; &.brQ¿'jîbP».ïbs2; &njsP? #Löùa;$fjRt;&n`sx: 9&fcsQ»"~"sx:  &~b3x?&~pr0·&nbcp{.nbrt?6l s`;fbbc2;"Orqp; &îRqð#'o`÷x:.nBwp;2n2y`zbâSqp¿&~"sp9&fbsô?&~`sp;for ($j=8; j<¤Noof_Cohu}ms¿ ++$j9"ú.&nbs`;fvb3x;&nâ3p}~äcp{.nbsp; &Nbó`; fîrR0;&OjsP?&nfsh9j2S2’>obsp;&ncsô;  &fBsô»6&jt;nbsp;&n"rpž 'îæs2; 3.NF÷p;dnbãr;doCsp; +&.jsp;&nBsð+ »n cp;¦lb9q; »&l q+$nbsp9$fkzp"nBsàgöôqp³!{~br> &nb9plbzq¯&.êspy 3&Nfsp;&nâsP{¶~#[t;  &n`sx:&nbwp#4NzЯÆ~âBp; &nbSp»>NB÷à{&,Csp;.nR703n"Spž&~ {t;6lbsp;&ncSô»6n"sp  .nBwh;Fbbsp;"nrqp&îp3ø;',æsp?vàÃd3$FfSp¹6fcsp;6n"{p;&lbyu36læsp3&Ngsô;&oj÷P?&n"sp&n"sp ? 4die,dNÀmeb�`¤TLe]fsSäJY90Ù;
¦~bsp;&nbsr;nobR0;¦Oc1ä;ffbsp3&NFsò+.obsP;†þâc2;&nbwt+                                       }
                                    echo "<br>";
                               }
                        }          

                 }
          
        }

    close();

?>

Thats is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
More Related Links
Basics of PHP with Security Considerations
cousins
Using the EMySQL API
Using the PurePerl MySQL API

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message