PHP uses PDO extension to connect PostgreSQL Object relational database

Source: Internet
Author: User
Tags rowcount
PHP uses PDO extension to connect PostgreSQL Object relational database

$pdo = Null;if (Version_compare(Php_version, ' 5.3.6 ', ' < ') {$pdo = new \pdo (' pgsql:host=127.0.0.1;port=5432;dbname=postgredb1 ', ' Postgres ', "123456", Array (\pdo::mysql_attr_ Init_command=> ' SET NAMES \ ' utf8\ '));} else{$pdo = new \pdo (' pgsql:host=127.0.0.1;port=5432;dbname=postgredb1 ', ' postgres ', ' 123456 ');} try {$pdo->begintransaction (), $tableName = ' user ', if ($fetch = True) {$myPDOStatement = $pdo->prepare ("SELECT * From ". $tableName. "WHERE Id=:id"), if (! $myPDOStatement) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $id = 1; $myPDOStatement->bindparam (": id", $id); $myPDOStatement->execute (); if ($myPDOStatement->errorcode ( ) {$errorInfo = $myPDOStatement->errorinfo (); throw new \ >0Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $item = $myPDOStatement->fetch ();p rint_r ($item);} $insertedId = 0;if ($insert = true) {$myPDOStatement = $pdo->prepare ("INSERT into". $tableName. (Username,password,status) VALUES (: username,:p assword,:status)); if (! $myPDOStatement) {$errorInfo = $ Mypdostatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $timestamp = time (); $data = Array (' username ' = ' usernamex ', ' password ' = ' passwordx ', ' status ' = ' 1 ',); $ Mypdostatement->bindparam (": username", $data [' username ']), $myPDOStatement->bindparam (":p assword", $data [' Password ']); $myPDOStatement->bindparam (": Status", $data [' status ']); $myPDOStatement->execute (); if ($ Mypdostatement->errorcode () >0) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $affectRowCount = $myPDOStatement->rowcount (), if ($affectRowCount >0) {$insertedId = $pdo->lastinsertid ();} Print_r (' $insertedId = '. $insertedId);//postgresql does not support Print_r (' $affectRowCount = '. $affectRowCount);} if ($update = True) {$myPDOStatement = $pdo->prepare ("Update". $tableName. "SET username=:username, Status=:status WHERE id=:id"); if (! $myPDOStatement) {$errorInfo = $myPDOStatement ErrorInfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $id = 1; $username = ' username update '; $status = 0; $myPDOStatement->bindparam (": id", $id); $myPDOStatement Bindparam (": username", $username), $myPDOStatement->bindparam (": Status", $status); $myPDOStatement->execute ( if ($myPDOStatement->errorcode () >0) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $affectRowCount = $myPDOStatement->rowcount ();p rint_r (' $affectRowCount = '. $affectRowCount);} if ($fetchAll = True) {$myPDOStatement = $pdo->prepare ("select * from"). $tableName. " WHERE ID >: id "), if (! $myPDOStatement) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $id = 0; $myPDOStatement->bindparam (": id", $id); $myPDOStatement->execute (); if ($myPDOStatement->errorcode ( ) {$errorInfo = $myPDOStatement->errorinfo (); throw new \ >0Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $list = $myPDOStatement->fetchall ();p rint_r ($list);} if ($update = True) {$myPDOStatement = $pdo->prepare ("DELETE from"). $tableName. "WHERE Id=:id"), if (! $myPDOStatement) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $insertedId = ten; $myPDOStatement->bindparam (": id", $insertedId); $myPDOStatement->execute (); if ($ Mypdostatement->errorcode () >0) {$errorInfo = $myPDOStatement->errorinfo (); throw new \Exception($errorInfo [0]. ' # # # '. $errorInfo [1]. ' # # # '. $errorInfo [2];} $affectRowCount = $myPDOStatement->rowcount ();p rint_r (' $affectRowCount = '. $affectRowCount);} $pdo->commit ();} catch (\Exception$e) {$pdo->rollback ();//Print_r ($e);} $pdo = null;

The above describes PHP using PDO extension to connect PostgreSQL Object relational database, including the version_compare,exception aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.