<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页实例</title>
<style>
.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:10px; margin-left:1px; font-family:Verdana;color:#000000;}
.page A.currentpage{ color:#FF0000; border: solid red 1px;}
.page A:hover{ background-color:#CCCCCC;}
</style>
</head>
<body><?php
error_reporting(7);
//初始化翻页链接参数
$page=$_GET["page"]+0; //思考这里是用强制转换为整数还是用什么方法呢
$page= $page<=0 ? 1 : $page; //避免$_GET["page"]为负数和0的情况
//分页尺寸
$page_size=10;
$serverName = "(local)"; //数据库服务器地址
$uid = "sa"; //数据库用户名
$pwd = "ulptfstx"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"MSD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false) {
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
//计算总行数
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
//总页数
$total_page=ceil($total_records/$page_size); //进一法取整,避免最后一页不满$page_size,
$page=$page>$total_page?$total_page:$page; //避免$_GET["page"]大于总页数
//翻页链接开始
$page_link="";
if ($total_page>1)
{
$page_link="<a>一共{$total_page}页</a>";
$page_link.="<a>当前第{$page}页</a>";
$page_link.="<a href=?page=1>首页</a>";
if ($page>1){
//页码大于1的时候,显示上一页翻页链接
$pre_page=$page-1;
$page_link.="<a href='?page=$pre_page'><<</a>";
}
//翻页列表
for ($i=1;$i<=$total_page;$i++)
{
if ($i==$page)
{
//高亮当前页页码
$page_link.="<a href='?page=$i' class='currentpage'>$i</a>";
}
else
{
$page_link.="<a href='?page=$i'>$i</a>";
}
}
//页码小于总页数的时候显示下一页翻页链接
$next_page=$page+1;
if($next_page<$total_page)
{
$page_link.="<a href='?page={$next_page}'>>></a>";
}
$page_link.="<a href='?page=$total_page'>最后一页</a>";
}
//输出记录
$offset=($page-1)*$page_size;
$query=sqlsrv_query("select * from INVMB limit $offset,$page_size") or die(sqlsrv_error());
echo "<ul>";
while ($rs=sqlsrv_fetch_array($query))
{
echo "<li>{$rs["0"]}-{$rs["1"]}</li>";
}
echo "<ul>";
echo "<div class='page'>$page_link</div>";
?>
</body>
</html>
这段代码来自 http://topic.csdn.net/u/20120117/11/c2618d08-b88c-4586-8b00-f1938f5c7792.html
怎么把它改成连接SQLServer的,现在运行提示以下错误:
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
Warning: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 77
Fatal error: Call to undefined function sqlsrv_error() in D:\Apache server\Apache\htdocs\bbs\demo.php on line 77
13 个解决方案
#1
#2
"select * from INVMB limit $offset,$page_size"
首先sql语句必须改了
mssql没有limit的
用top
首先sql语句必须改了
mssql没有limit的
用top
#3
可以用TOP吗?
limit可以这么写:limit 10,20,可TOP不行吧
limit可以这么写:limit 10,20,可TOP不行吧
#4
请问用top应该怎么写?
#5
适用于sqlserver、oracle的通用分页查询
你有 select * from INVMB limit $offset,$page_size
所以
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$sqls 就是执行用的串
你有 select * from INVMB limit $offset,$page_size
所以
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$sqls 就是执行用的串
#6
版主,能不能结合我前面的代码改好一起贴出来啊。 感激不尽!!!
#7
把 $query=sqlsrv_query("select * from INVMB limit $offset,$page_size")
替换成
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls)
替换成
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls)
#8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页实例</title>
<style>
.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:10px; margin-left:1px; font-family:Verdana;color:#000000;}
.page A.currentpage{ color:#FF0000; border: solid red 1px;}
.page A:hover{ background-color:#CCCCCC;}
</style>
</head>
<body><?php
error_reporting(7);
//初始化翻页链接参数
$page=$_GET["page"]+0; //思考这里是用强制转换为整数还是用什么方法呢
$page= $page<=0 ? 1 : $page; //避免$_GET["page"]为负数和0的情况
//分页尺寸
$page_size=10;
$serverName = "(local)"; //数据库服务器地址
$uid = "sa"; //数据库用户名
$pwd = "ulptfstx"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"MSD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false) {
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
//计算总行数
$total_records=sqlsrv_num_rows($conn,sqlsrv_query("SELECT MB001 from INVMB"));
//总页数
$total_page=ceil($total_records/$page_size); //进一法取整,避免最后一页不满$page_size,
$page=$page>$total_page?$total_page:$page; //避免$_GET["page"]大于总页数
//翻页链接开始
$page_link="";
if ($total_page>1)
{
$page_link="<a>一共{$total_page}页</a>";
$page_link.="<a>当前第{$page}页</a>";
$page_link.="<a href=?page=1>首页</a>";
if ($page>1){
//页码大于1的时候,显示上一页翻页链接
$pre_page=$page-1;
$page_link.="<a href='?page=$pre_page'><<</a>";
}
//翻页列表
for ($i=1;$i<=$total_page;$i++)
{
if ($i==$page)
{
//高亮当前页页码
$page_link.="<a href='?page=$i' class='currentpage'>$i</a>";
}
else
{
$page_link.="<a href='?page=$i'>$i</a>";
}
}
//页码小于总页数的时候显示下一页翻页链接
$next_page=$page+1;
if($next_page<$total_page)
{
$page_link.="<a href='?page={$next_page}'>>></a>";
}
$page_link.="<a href='?page=$total_page'>最后一页</a>";
}
//输出记录
$offset=($page-1)*$page_size;
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls);
echo "<ul>";
while ($rs=sqlsrv_fetch_array($query))
{
echo "<li>{$rs["0"]}-{$rs["1"]}</li>";
}
echo "<ul>";
echo "<div class='page'>$page_link</div>";
?>
</body>
</html>
是这样子吗?但还是报错:
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
Fatal error: Param count and argument count don't match. in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
#9
建议你多看手册
mixed sqlsrv_num_rows ( resource $stmt )
mixed sqlsrv_query ( resource $conn , string $sql [, array $params [, array $options ]] )
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
mixed sqlsrv_num_rows ( resource $stmt )
mixed sqlsrv_query ( resource $conn , string $sql [, array $params [, array $options ]] )
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
#10
例如
$pagesize = 10;
$p = (当前页-1)*$pagesize;
"select top $pagesize from INVMB where 表的唯一标识符 not in(select top $p 表的唯一标识符 from INVMB order by 需要排序的字段) order by 需要排序的字段"
#11
可以加我的QQ吗?谢谢
#12
把QQ告诉我
#13
497930813
#1
#2
"select * from INVMB limit $offset,$page_size"
首先sql语句必须改了
mssql没有limit的
用top
首先sql语句必须改了
mssql没有limit的
用top
#3
可以用TOP吗?
limit可以这么写:limit 10,20,可TOP不行吧
limit可以这么写:limit 10,20,可TOP不行吧
#4
请问用top应该怎么写?
#5
适用于sqlserver、oracle的通用分页查询
你有 select * from INVMB limit $offset,$page_size
所以
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$sqls 就是执行用的串
你有 select * from INVMB limit $offset,$page_size
所以
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$sqls 就是执行用的串
#6
版主,能不能结合我前面的代码改好一起贴出来啊。 感激不尽!!!
#7
把 $query=sqlsrv_query("select * from INVMB limit $offset,$page_size")
替换成
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls)
替换成
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls)
#8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分页实例</title>
<style>
.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:10px; margin-left:1px; font-family:Verdana;color:#000000;}
.page A.currentpage{ color:#FF0000; border: solid red 1px;}
.page A:hover{ background-color:#CCCCCC;}
</style>
</head>
<body><?php
error_reporting(7);
//初始化翻页链接参数
$page=$_GET["page"]+0; //思考这里是用强制转换为整数还是用什么方法呢
$page= $page<=0 ? 1 : $page; //避免$_GET["page"]为负数和0的情况
//分页尺寸
$page_size=10;
$serverName = "(local)"; //数据库服务器地址
$uid = "sa"; //数据库用户名
$pwd = "ulptfstx"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"MSD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false) {
echo "连接失败!";
die( print_r( sqlsrv_errors(), true));
}
//计算总行数
$total_records=sqlsrv_num_rows($conn,sqlsrv_query("SELECT MB001 from INVMB"));
//总页数
$total_page=ceil($total_records/$page_size); //进一法取整,避免最后一页不满$page_size,
$page=$page>$total_page?$total_page:$page; //避免$_GET["page"]大于总页数
//翻页链接开始
$page_link="";
if ($total_page>1)
{
$page_link="<a>一共{$total_page}页</a>";
$page_link.="<a>当前第{$page}页</a>";
$page_link.="<a href=?page=1>首页</a>";
if ($page>1){
//页码大于1的时候,显示上一页翻页链接
$pre_page=$page-1;
$page_link.="<a href='?page=$pre_page'><<</a>";
}
//翻页列表
for ($i=1;$i<=$total_page;$i++)
{
if ($i==$page)
{
//高亮当前页页码
$page_link.="<a href='?page=$i' class='currentpage'>$i</a>";
}
else
{
$page_link.="<a href='?page=$i'>$i</a>";
}
}
//页码小于总页数的时候显示下一页翻页链接
$next_page=$page+1;
if($next_page<$total_page)
{
$page_link.="<a href='?page={$next_page}'>>></a>";
}
$page_link.="<a href='?page=$total_page'>最后一页</a>";
}
//输出记录
$offset=($page-1)*$page_size;
$sql = 'select * from INVMB';
$start = $offset;
$pagesize = $page_size;
$sqls =<<< SQL
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM ($sql) A
WHERE ROWNUM < $start+$pagesize
)
WHERE RN >= $start
SQL;
$query=sqlsrv_query($sqls);
echo "<ul>";
while ($rs=sqlsrv_fetch_array($query))
{
echo "<li>{$rs["0"]}-{$rs["1"]}</li>";
}
echo "<ul>";
echo "<div class='page'>$page_link</div>";
?>
</body>
</html>
是这样子吗?但还是报错:
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
Fatal error: Param count and argument count don't match. in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33
#9
建议你多看手册
mixed sqlsrv_num_rows ( resource $stmt )
mixed sqlsrv_query ( resource $conn , string $sql [, array $params [, array $options ]] )
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
mixed sqlsrv_num_rows ( resource $stmt )
mixed sqlsrv_query ( resource $conn , string $sql [, array $params [, array $options ]] )
$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));
#10
例如
$pagesize = 10;
$p = (当前页-1)*$pagesize;
"select top $pagesize from INVMB where 表的唯一标识符 not in(select top $p 表的唯一标识符 from INVMB order by 需要排序的字段) order by 需要排序的字段"
#11
可以加我的QQ吗?谢谢
#12
把QQ告诉我
#13
497930813