要浏览表中的记录需要执行以下几步操作:
(1) 执行以下SQL语句:
select field1,field2,field3,...from $table_name
其中field1,field2,field3表示的是需要从表中查询的字段名称:$table_name为表的名称。
(2)使用PHP MSSQL类库中的mssql_fetch_array()函数读取执行SQL语句所返回的结果集。该函数的语法格式如下代码:
mssql_fetch_array(resource result [,int result_type])
其中参数result为函数mssql_query()所执行的SQL结果集;参数result_type为返回结果数组的类型,默认为既可以用数值也可以用字段名作数组元素。该函数将返回一个结果数组。
另外还可以用mssql_num_rows()函数来获取结果集中记录的数目。该函数格式:
mssql_num_rows(resource result)
以上代码中参数result为函数mssql_query()所执行的sql结果集。该函数将根据结果集的行数,返回相应的数值。
(3)通过while循环读出结果集中的每条记录。
<?php
$hostname="datahost";
$dbuser="sa";
$dbpasswd="123";
$dbname="tempdb";
$conn=mssql_connect($datahost,$dbuser,$dbpasswd);
mssql_select_db($dbname);
$t_name="my_table";
$select_t="select id,name from $t_name";
$result=mssql_query($select_t,$conn);
$re_num=mssql_num_rows($result);
echo"<font color=#666666>表中目前共有记录</font>";
echo "<strong><font color=green>".$re_num."</font></strong>";
echo "<font color=#666666>条</font>";
echo "<p>";
while ($rows=mssql_fetch_array($result))
{
echo "<font color=#666666>编号:</font><strong><font color=green>".$rows[id]."</font></strong>";
echo "<font color=#666666>姓名:</font><strong><font color=green>".$rows[name]."</font></strong>";
echo "<p>";
}
mssql_close($conn);
?>