易截截图软件、单文件、免安装、纯绿色、仅160KB

php+mysql分页


分页功能的实现是每种WEB开发语言必须要实现的功能。PHP也好,JSP也罢。我准备用两个方法来阐述PHP+MYSQL实现分页的功能。
 
一、分页程序的原理
分页程序有两个非常重要的参数:每页显示几条记录($pagesize)和当前是第几页($page)。有了这两个参数就可以很方便的写出分页程序,我们以MySql数据库作为数据源,在mysql里如果要想取出表内某段特定内容可以使用的SQL语句:select * from table limit offset,rows来实现。这里的offset是记录偏移量,它的计算方法是offset(下标意思)=$pagesize*($page-1),rows是要显示的记录条数,这里就是$page。也就是说select * from table limit 10,10这条语句的意思是取出表里从第11条记录开始的20条记录。
 
二、主要代码解析
<?php
$conn=mysql_connect("localhost","root","123");//连接数据库
$rs=mysql_query("select count(*) from tb_product",$conn); //取得记录总数$rs
$pagesize=10; //设置每一页显示的记录数
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
$pages=intval($numrows/$pagesize);//计算总页数
 
 
?>
 
三 完整代码
<html>
<head>
<title>php分页示例</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<?php
 $conn=mysql_connect("localhost","root","");
 //设定每一页显示的记录数
 $pagesize=1;
 mysql_select_db("mydata",$conn);
 //取得记录总数$rs,计算总页数用
 $rs=mysql_query("select count(*) from tb_product",$conn);
 $myrow = mysql_fetch_array($rs);
 $numrows=$myrow[0];
 //计算总页数
 $pages=intval($numrows/$pagesize);
 if ($numrows%$pagesize)
  $pages++;
 //设置页数
 if (isset($_GET['page'])){
  $page=intval($_GET['page']);
 }
 else{
  //设置为第一页
  $page=1;
 }
 //计算记录偏移量
 $offset=$pagesize*($page - 1);
 //读取指定记录数
 $rs=mysql_query("select * from myTable order by id desc limit $offset,$pagesize",$conn);
 if ($myrow = mysql_fetch_array($rs))
 {
  $i=0;
  ?>
  <table border="0" width="80%">
  <tr>
   <td width="50%" bgcolor="#E0E0E0">
    <p align="center">标题</td>


相关文档:

PHP模拟多线程方法总结

PHP语言本身是不支持多线程的. 总结了一下网上关于PHP模拟多线程的方法, 总的来说, 都是利用了PHP的好伙伴们本身所具有的多线程能力.
PHP的好伙伴指的就是LINUX和APACHE啦, LAMP嘛.
另外, 既然是模拟的, 就不是真正的多线程. 其实只是多进程. 进程和线程是两个不同的概念. 好了, 以下方法都是从网上找来的.
1. 利用LIN ......

PHP Floating point precision

Floating point precision
It is typical that simple decimal fractions like 0.1
or
0.7
cannot be converted into their internal binary
counterparts without a small loss of precision. This can lead to
confusing
results: for example, floor((0.1+0.7)*10)
will usually
return 7
......

PHP初学者头痛的十四个问题

1.页面之间无法传递变量
get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_POST['foo'],$_SESSION['foo']来得到。当然也可以修改自动全局变量为开(php.ini改为register_globals = On);考虑到兼容性,还是强迫自己熟悉新的写法比较好。
 
2.Win32 ......

php mysql 配置php.ini

文件php.ini放入windows下,将下面内容拷贝到记事本命名为php.ini放入c:/windows下,重启Apache server:
[PHP]
;;;;;;;;;;;
; WARNING ;
;;;;;;;;;;;
; This is the default settings file for new PHP installations.
; By default, PHP installs itself with a configuration suitable for
; development purposes ......

php session配置

Session]
; 除非使用session_register()或$_SESSION注册了一个变量。
; 否则不管是否使用了session_start(),都不会自动添加任何session记录。
; 包括resource变量或有循环引用的对象包含指向自身的引用的对象,不能保存在会话中。
; register_globals指令会影响到会话变量的存储和恢复。
session ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号