PHPmysql数据库操作实例学习
时间:2021-07-01 10:21:17
帮助过:20人阅读
1,获取表数据01
- mysql_connect("localhost","dev","mysql");
- mysql_select_db("dev");
- $result=mysql_query("select id,name from tb_test");
- while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
- print_r($row);echo "
\n"; - }
- ?>
输出结果:
2,获取表数据02
- mysql_connect("localhost","dev","mysql");
- mysql_select_db("dev");
- $result=mysql_query("select id,name from tb_test");
- while($row=mysql_fetch_object($result)){
- echo "ID:" .$row->id." text: ".$row->name."
\n"; - }
- ?>
输出结果:
3,获取表数据03
- mysql_connect("localhost","dev","mysql");
- mysql_select_db("dev");
-
- $result=mysql_query("select id,name from tb_test");
- print "
";- print "
编码 | "; - print "
名字 | "; -
- while ($row = mysql_fetch_array($result))
- {
- print "
";- print "
{$row["id"]} | "; - print "
{$row["name"]} | "; - }
- print "
"; - ?>
输出结果:
4,获取数据分页04
- $page=isset($_GET['page'])?intval($_GET['page']):1;
- $num=3; //每页显示3条数据
- $db=mysql_connect("localhost","dev","mysql");
- $select=mysql_select_db("dev",$db);
- $total=mysql_num_rows(mysql_query("select id,name from tb_test"));
- $pagenum=ceil($total/$num);
- If($page>$pagenum || $page == 0){
- Echo "Error : Can Not Found The page .";
- Exit;
- }
- $offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
- $info=mysql_query("select id,name from tb_test limit $offset,$num");
- print "
";- print "
编码 | "; - print "
名字 | "; - While($row=mysql_fetch_array($info)){
- print "
";- print "
";- print "
{$row["id"]} | "; - print "
{$row["name"]} | "; - }//显示数据
- print "
"; - For($i=1;$i<=$pagenum;$i++){
- $show=($i!=$page)?"$i":"$i";
- Echo $show." ";
- }
- ?>
输出结果:
|