I can tell u how is in mysql, I supose is the same with postgresql, but just use anoher function ...

$sql1 = "blablablablabla";
mysql_query($sql1);

$sql2 = "innie minnie minnie moh";
mysql_query($sql2);

$sql3 = "xptosqrttimestwo";
mysql_query($sql3);

U should check phpmanual ... 😉

    well... that code is the same as mine.

    this dont come in the php manual... this is a postgresql question

    mysql didnt support transactions until version 5...
    that code u posted is the execution of three queries, but i need to know how to start and commit/rollback a transaction.

    can anybody post a sample of a postgres transaction using php?

      bogu wrote:

      Sry, my bad ....

      I made a little search here on the site, and look what I find, I hope it helps you ...

      http://www.phpbuilder.com/columns/linuxjournal200009.php3

      cooooooooooooooolll!!

      thats a great value piece of documentation... i will save this article in my computer as it is so valuable and rare.

      when it comes to documentation postgres is really behind mysql.

      thank you very much... that article has exacly what i needed to know

        I'm glad that I could help. 😉

          ok i have to come and bring this thread up again...

          in that article they use pg_exec()

          i went on the manual and there is no such a function... is it like the old version of pg_query() or something?

            pg_exec is the old school version of pg_query. It's deprecated, but apparently still there in php 5.1.2

              A few more points...

              In PostgreSQL, transactions are generally all or nothing. Note that with version 8.0 and above, you have save points. You can use these to roll back part of a transaction to it's previous state. For instance, in the older versions (7.4 and before) if you did this:

              begin;
              insert into tablea ...
              insert into tableb ...
              insert into tablec ... <-- get an error here
              rollback / commit;

              the whole transaction will be rolled back, no way around it. Using rollback or commit would make no difference, as you got an error.

              With the newer version, you can do something like:

              begin;
              insert into tablea ...
              insert into tableb ...
              savepoint backhere;
              insert into tablec ... <-- get an error here
              (in php: )
              if error then "rollback to backhere", execute update instead of insert
              commit;

              See this page for information on savepoints:

              http://www.postgresql.org/docs/8.1/interactive/sql-savepoint.html

                First of all thank you very much for the time and effort helping me. Its apreciated.

                yep... i have read that page of the manual before posting any questions here...

                my doubts are more in how to execute transactions within a php script... like... does this code looks ok?

                pg_query("begin");
                
                pg_query("insert into tablea ...");
                
                pg_query("insert into tableb ...");
                
                pg_query("delete from tablea where....");
                
                if (...)    //dont really know how to detect an error
                {
                pg_query("rollback to backhere");  //this sintax should be wrong
                }
                pg_query("commit"); 

                  Yep, you've pretty much got it all down. You can just check to see if the result is false of the pg_query:

                  $res = pg_query ("insert into sometable");
                  if (!$res){
                      pg_query("rollback to backthere");
                  } else {
                      pg_query("update sometable set.... where id=$id");
                  }
                  pg_query("commit");
                  

                  And you've done it

                    Sxooter wrote:

                    In PostgreSQL, transactions are generally all or nothing.

                    Isn't that how transactions generally are in all RDBMS [rollback undoes the entire transaction] unless you explicitly set save points or perform nested transactions?

                      Sxooter wrote:

                      Yep, you've pretty much got it all down. You can just check to see if the result is false of the pg_query:

                      $res = pg_query ("insert into sometable");
                      if (!$res){
                          pg_query("rollback to backthere");
                      } else {
                          pg_query("update sometable set.... where id=$id");
                      }
                      pg_query("commit");
                      

                      And you've done it

                      thanks a brunch!

                      lets put this thing working!

                        Write a Reply...