5、PHP程序开发
5.1、连接数据库
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n<BR>"; ;
exit;
}else{
echo "连接数据库成功"."\n<BR>";
}
//关闭连接
pg_close($conn);
?>
执行
[root@VM_0_47_centos test]# curl http://127.0.0.1:8080/dbsta/test/conn.php
连接数据库成功
5.2、创建数据表
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "连接数据库成功"."\n";
}
//建立数据表
$sql="create table public.tbase(id integer,nickname varchar(100)) distribute by shard(id) to group default_group;";
$result = @pg_exec($conn,$sql) ;
if (!$result){
$error_msg=@pg_errormessage($conn);
echo "创建数据表出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "创建数据表成功"."\n";
}
//关闭连接
pg_close($conn);
?>
执行
[root@VM_0_47_centos test]# curl http://127.0.0.1:8080/dbsta/test/createtable.php
连接数据库成功
创建数据表成功
5.3、插入数据
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "连接数据库成功"."\n";
}
//插入数据
$sql="insert into public.tbase values(1,'tbase'),(2,'pgxz');";
$result = @pg_exec($conn,$sql) ;
if (!$result){
$error_msg=@pg_errormessage($conn);
echo "插入数据出错,详情:".$error_msg."\n";
exit;
}else{
echo "插入数据成功"."\n";
}
//关闭连接
pg_close($conn);
?>
执行
[tbase@VM_0_47_centos test]$ curl http://127.0.0.1:8080/dbsta/test/insert.php
连接数据库成功
插入数据成功
5.4、查询数据
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "连接数据库成功"."\n";
}
//查询数据
$sql="select id,nickname from public.tbase";
$result = @pg_exec($conn,$sql) ;
if (!$result){
$error_msg=@pg_errormessage($conn);
echo "查询数据出错,详情:".$error_msg."\n";
exit;
}else{
echo "插入数据成功"."\n";
}
$record_num = pg_numrows($result);
echo "返回记录数".$record_num."\n";
$rec=pg_fetch_all($result);
for($i=0;$i<$record_num;$i++){
echo "记录数#".strval($i+1)."\n";
echo "id:".$rec[$i]["id"]."\n";
echo "nickname:".$rec[$i]["nickname"]."\n\n";
}
//关闭连接
pg_close($conn);
?>
调用方法
[root@VM_0_47_centos ~]# curl http://127.0.0.1:8080/dbsta/test/select.php
连接数据库成功
插入数据成功
返回记录数2
记录数#1
id:1
nickname:tbase
记录数#2
id:2
nickname:pgxz
5.5、流数据copy 入表
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "连接数据库成功"."\n";
}
$row=ARRAY("1,TBase","2,pgxz");
$flag=pg_copy_from($conn,"public.tbase",$row,",");
if (!$flag){
$error_msg=@pg_errormessage($conn);
echo "copy出错,详情:".$error_msg."\n";
}else{
echo "copy成功"."\n";
}
//关闭连接
pg_close($conn);
?>
调用方法
curl http://127.0.0.1/dbsta/cron/php_copy_from.php
连接数据库成功
copy成功
5.6、copy to导出数据到一个数组中
<?php
$host="172.16.0.29";
$port="15432";
$dbname="postgres";
$user="tbase" ;
$password="";
//连接数据库
$conn=@pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn){
$error_msg=@pg_errormessage($conn);
echo "连接数据库出错,详情:".$error_msg."\n"; ;
exit;
}else{
echo "连接数据库成功"."\n";
}
$row=pg_copy_to($conn,"public.tbase",",");
if (!$row){
$error_msg=@pg_errormessage($conn);
echo "copy出错,详情:".$error_msg."\n";
}else{
print_r($row);
}
//关闭连接
pg_close($conn);
?>
调用方法
curl http://127.0.0.1/dbsta/cron/php_copy_to.php
连接数据库成功
Array
(
[0] => 1,TBase
[1] => 2,pgxz
)