未加星标

Getting Started With Using PostgreSQL in PHP

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

The first thing you need to do is installphp and support for PostgreSQL. For ubuntu 16.04 you would need to run:

sudo apt-get install php7.0 php7.0-pgsql -y

If you haven't already installed a PostgreSQL server andconfigured it with a password and to allow remote connections, then do that now.

The script below demonstrates how to:

connect to the PostgreSQL server. insert some data grab some data and loop over the results.

Copy and paste the script into a file called script.php before executing it with php script.php .

<?php define('DB_HOST', '10.2.7.144'); define('DB_NAME', 'test'); define('DB_PORT', '5432'); define('DB_USER', 'postgres'); define('DB_PASSWORD', 'hickory2000'); function getConnection($host, $db_name, $user, $password, $port="5432", $use_utf8=true, $force_new=false, $useAsync=false) { if ($force_new && $useAsync) { $force_new = false; } $connString = "host=" . $host . " dbname=" . $db_name . " user=" . $user . " password=" . $password . " port=" . $port; if ($use_utf8) { $connString .= " options='--client_encoding=UTF8'"; } print $connString . PHP_EOL; if ($useAsync) { $connection = pg_connect($connString, PGSQL_CONNECT_ASYNC); } elseif ($force_new) { $connection = pg_connect($connString, PGSQL_CONNECT_FORCE_NEW); } else { $connection = pg_connect($connString); } if ($connection == false) { throw new Exception("Failed to initialize database connection."); } return $connection; } $connection = getConnection(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD); $createTableQuery = "CREATE TABLE broker ( id serial NOT NULL, name varchar(255) NOT NULL, redirect_url text NOT NULL, secret varchar(30) NOT NULL, modified_timestamp timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) )"; $result = pg_query($connection, $createTableQuery); $insertBrokersQuery = "INSERT INTO broker (name, redirect_url, secret) VALUES " . "('google', 'www.google.com', 'fdg3rgdf3435fhgfvgdf')" . ", ('yahoo', 'www.yahoo.com', 'dfdfweregf33434gdfdd')"; $result = pg_query($connection, $insertBrokersQuery); $selectBrokersQuery = "SELECT * FROM broker"; $result = pg_query($connection, $selectBrokersQuery); if (!$result) { echo "An error occurred.\n"; print pg_last_error($connection); exit; } while ($row = pg_fetch_assoc($result)) { print "row: " . print_r($row, true) . PHP_EOL; } print "done" . PHP_EOL;

After reading and executing the script, you should see that it's very much like using mysqli with fetched rows having name/value form.

Tags:php, PostgreSQL

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: SQLPHPPostgreSQLAUUTCUTIRYRIM
分页:12
转载请注明
本文标题:Getting Started With Using PostgreSQL in PHP
本站链接:http://www.codesec.net/view/481251.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(27)