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.