首页登陆欢迎您!
首页登陆 > 编程 > 二款PostgreSQL连接数据的方法和操作实例

二款PostgreSQL连接数据的方法和操作实例

时间:2019-12-18

本文实例陈说了php连接与操作PostgreSQL数据库的法子。共享给我们供我们参谋。

 代码如下

1. 确立和停业连接 1) mysql_connect() resource mysql_connect([string hostname [:port][:/path/to/socket][,string username] [,string password]]卡塔尔(قطر‎ 全部参数都是可选的 比方: @mysql_connect(“localhost”, “user”, “password”) or die(“Could not connect to mysql server!”卡塔尔国; 注意,@符号表示防止退步尝试招致的别的错误音讯,顾客将看到的是die(卡塔尔(قطر‎中钦点的荒诞消息. 注意,当与八个mysql举办连接时,必得钦命各种连接的链接ID,如下: $link1 = @mysql_connect(“server1″, “user”, “password”) or die(“Could not connect to mysql server!”); $link2 = @mysql_connect(“server2″, “user”, “password”) or die(“Could not connect to mysql server!”); 2) mysql_pconnect() resource mysql_pconnect([string hostname [:port][:/path/to/socket][,string username] [,string password]]) 与mysql_connect(卡塔尔(英语:State of Qatar)分化的是:会首先查找现成链接,不设有的时候才创立. 注意,无需展现关闭连接,因为接二连三将放在池中,所以叫长久连接. 3卡塔尔(英语:State of Qatar)mysql_close() boolean mysql_close 关闭连接不是必得的,因为能够由mysql的废料回笼来管理. 如果未有钦赐link_id,则关闭近期的链接. 2. 选项数据库 mysql_select_db() boolean mysql_select_db(string db_name [, resource link_id]) 3. 查询MySql 1) mysql_query() resource mysql_query(string query [,resource link_id]卡塔尔(英语:State of Qatar) 肩负施行query. 2卡塔尔国mysql_db_query() resource mysql_db_query(string database, string query [, resource link_id]) 等价于mysql_select_db,从参数中就能够以看见道的收看来. 4. 到手和显示数据 1) mysql_result() mixed mysql_result(resource result_set, int row [,mixed field]) 从result_set 的内定row 中得到一个田野先生 的数据. 简单可是功用低. 比方: 复制代码 代码如下: $link1 = @mysql_connect(“server1″, “webuser”, “password”) or die(“Could not connect to mysql server!”); @mysql_select_db or die(“Could not select database!”); $query = “select id, name from product order by name”; $result = mysql_query; $id = mysql_result; $name = mysql_result; mysql_close(卡塔尔国; 注意,上述代码只是出口结果集中的首先条数据的字段值,假若要出口全数记录,要求循环管理. 复制代码 代码如下: … for ($i = 0; $i <= mysql_num_rows { $id = mysql_result; $name = mysql_result; echo “Product: $name ”; } … 注意,若是查询字段名是别称,则mysql_result中就应用别称. 2卡塔尔(قطر‎mysql_fetch_row() array mysql_fetch_row 从result_set中获得整行,把多少纳入数组中. 比如: 复制代码 代码如下: … $query = “select id, name from product order by name”; $result = mysql_query; while = mysql_fetch_row { echo “Product: $name mysql_fetch_array() array mysql_fetch_array(resource result_set [,int result_type]) mysql_fetch_row(卡塔尔(قطر‎的增加版. 将result_set的每意气风发行获取为一个关联数组或/和数值索引数组. 暗中同意获取三种数组,result_type能够设置: MYSQL_ASSOC:再次来到关联数组,字段名=>字段值 MYSQL_NUM:再次来到数值索引数组. MYSQL_BOTH:获取两种数组.由此各样字段可以按索引偏移援用,也得以按字段名援用. 比方: 复制代码 代码如下: … $query = “select id, name from product order by name”; $result = mysql_query; while($row = mysql_fetch_array { $name = $row['name'];//或者 $name = $row[1]; $name = $row['id'];//或者 $name = $row[0]; echo “Product: $name mysql_fetch_assoc() array mysql_fetch_assoc 相当于 mysql_fetch_array 5) mysql_fetch_object() object mysql_fetch_object 和mysql_fetch_array(卡塔尔国作用相像,不过再次来到的不是数组,而是一个对象. 举例: 复制代码 代码如下: … $query = “select id, name from product order by name”; $result = mysql_query; while($row = mysql_fetch_object { $name = $row->name; $name = $row->id; echo “Product: $name ”; } … 5. 所筛选的笔录和受影响的记录 1) mysql_num_rows() int mysql_num_rows 返回result_set中的行数. 注意,mysql_num_rows(卡塔尔(قطر‎只在显然select语句询问拿到的记录数有效,若是要收获insert/updata/delete查询影响的记录数,须求采纳mysql_affected_rows mysql_affected_rows() int mysql_affected_rows 获取insert/updata/delete查询影响的笔录数 注意,没有须求输入参数,私下认可使用以来树立的数据库连接的近年来结果.能够采纳可选参数link_id来抉择数据库连接. 6. 获取数据库和表的消息 1) mysql_list_dbs() resource mysql_list_dbs 获取服务器上独具数据库名称. 比如: 复制代码 代码如下: mysql_connect(“localhost”, “name”,”pwd”); $dbs = mysql_list_dbs = mysql_fetch_row { echo “$db
”; } 注意,输出结果与行使的客户权限相关. 2卡塔尔国 mysql_db_name() string mysql_db_name(resource result_set, interger index) 获取在mysql_list_dbs()返回的result_set中地方为index的数据库名. 3卡塔尔(英语:State of Qatar)mysql_list_tables() resource mysql_list_tables(string database [,resource link_id]卡塔尔 获取database中的全数表名. 4卡塔尔(قطر‎ mysql_tablename() string mysql_tablename(resource result_set, interger index) 获取mysql_list_tables()返回的result_set中地方为index的表名. 在读书PHP的COM 和 .Net函数的时候,开掘了三个因而COM操作SQL SE大切诺基VE奥德赛的事例,查找了相关的资料,于是就有了这篇PHP连接ACCESS的篇章,相信网络早已重重了,依然贴在这里处吧。 笔者的机械意况:WIN二〇〇〇,APACHE2,PHP Version 5.1.0RC1 复制代码 代码如下:

复制代码 代码如下:$pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes") or die("can't connect to database."); $query="select * from employes order by serial_no"; //$query="insert into employes values(10008,'susan','1985-09-04','80','50')"; $result=@pg_query or die("can't run query to table."); //echo pg_num_rows; //输出多少条记下被询问 //if //{ //echo "recrods inserted sucessfully!"; //echo pg_affected_rows;//输出多少条记下被插入 //} //实例风流罗曼蒂克[pg_fetch_row] echo "

$pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes")
or die("can't connect to database.");
$query="select * from employes order by serial_no";

Open(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\php5\netBook.mdb”); $rs = $conn->Execute(“select * from manage”); // 记录集 $num_columns = $rs->Fields->Count(); echo $num_columns . “
n”; for ($i=0; $i < $num_columns; $i++) { $fld[$i] = $rs->Fields; } $rowcount = 0; while { for ($i=0; $i < $num_columns; $i++) { echo htmlspecialchars . “t”; } echo “
n”; $rowcount++; // rowcount 自增 $rs->MoveNext; //关闭数据集 $conn->Close(卡塔尔(قطر‎; ?>

"; echo "

//$query="insert into employes values(10008,'susan','1985-09-04','80','50')";
$result=@pg_query($pg,$query) or die("can't run query to table.");
//echo pg_num_rows($result卡塔尔(英语:State of Qatar); //输出多少条记下被询问
//if($result)
//{
//echo "recrods inserted sucessfully!";
//echo pg_affected_rows($result卡塔尔;//输出多少条记下被插入
//}

"; echo "

//实例一[pg_fetch_row]
echo "<table border=1>";
echo "<tr>";
echo "<td>serial_no</td>";
echo"<td>name</td>";
echo"<td>birthday</td>";
echo"</tr>";

serial_no

for($i=0;$i<pg_num_rows($result);$i++)
{

"; echo"

$row=@pg_fetch_row($result) or die("can't fetch row from table.");
$serial_no= $row[0];
$name= $row[1];
$birthday= $row[2];
echo"<tr>";
echo"<td>$serial_no</td>";
echo"<td>$name</td>";
echo"<td>$birthday</td>";
echo"</tr>";

name

}
echo"</table>";

"; echo"

//实例二[pg_fetch_array]
//echo "<table border=1>";
//echo "<tr>";
//echo "<td>serial_no</td>";
//echo"<td>name</td>";
//echo"<td>birthday</td>";
//echo"</tr>";
//
//for($i=0;$i<pg_num_rows($result);$i++)
//{
//
//$row=@pg_fetch_array($result) or die("can't fetch row from table.");
//$serial_no= $row['serial_no'];
//$name= $row['name'];
//$birthday= $row['birthday'];
//echo"<tr>";
//echo"<td>$serial_no</td>";
//echo"<td>$name</td>";
//echo"<td>$birthday</td>";
//echo"</tr>";
//
//}
//echo"</table>";

birthday

//扩展,删除,改正实例
//$newrow=array("serial_no"=>"1006","name"=>"peter","birthday"=>"1990-07-03","salary"=>"90","bonus"=>"80");
//$reusult=@pg_insert($pg,"employes",$newrow) or die("can't insert data to table.");
//if($reusult)
//{
//echo "rechords inserted sucessfully!";
//}
//
pg_close($pg);

"; echo"

"; for($i=0;$i"; echo"

$serial_no

上一篇:php xml 入门上学材料_php手艺_脚本之家 下一篇:没有了