FreeBSD下Apache+PHP+PostgreSQL搭建
1、安装PostgreSQL
[1]cd /usr/ports/databases/postgresql82-server/
make install clean
默认安装。
[2]接下来初始化数据库:
mkdir /var/pgsql/data
chown pgsql /var/pgsql/data
#su pgsql
$initdb -D /var/pgsql/data
$ initdb -D /var/pgsql/data
$ postmaster -D /var/pgsql/data
[3]编辑/etc/rc.conf,添加:
postgresql_enable="YES"
postgresql_data="/var/pgsql/data"
postgresql_flags="-w -s -m fast"
2、安装Apache
cd /usr/ports/www/apache22
make install clean
默认安装,,网上文档常规设置,参考:
3、安装PHP
[1]cd /usr/ports/lang/php5
make install clean
[2]cd /usr/ports/lang/php5-extensions
make config
选择PGSQL支持
make install clean
记录起来很简单,摸索2天才搞定,至此,PHP可以成功连接并操作数据库。
附:
pgadmin创建表t_person后,测试PHP:
print "
Welcome!";
function db_connection(){
global $con;
$connection ="host=localhost user =username password=passme dbname=test";
$con = Pg_Connect($connection);
if( !$con ){
print "failed
Welcome
";
exit;
}
else{
print "success
Welcome
";
}
return $con;
}
$con = db_connection();
//////////////////////////////////INSERT///////////////////////////////
$sqlstr="INSERT INTO t_person(name,zip_code,city) VALUES('outcrop',1006,'GuangZhou')";
$insert=pg_exec($con,$sqlstr);
$sqlstr="SELECT * FROM t_person";
$select=pg_exec($con,$sqlstr);
$rows = pg_numrows($select);
/////////////////////////////SELECT/////////////////////////////
for($i=0;$i<$rows;$i++)
{
$data=pg_fetch_row($select,$i);
echo "$data[0], $data[1], $data[2]
\n";
}
if($insert==TRUE){echo"
Inserted!";}
else{echo"Insert failed";}
pg_close($con);
?>
阅读(856) | 评论(0) | 转发(0) |