查询记录

最近更新时间: 2024-10-17 17:10:00

<?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