name."\n"; }?>输出结果:3,获取表数据03 编码"; print "名字"; while ($row = mysql_fe">
当前位置:Gxlcms > PHP教程 > PHPmysql数据库操作实例学习

PHPmysql数据库操作实例学习

时间:2021-07-01 10:21:17 帮助过:20人阅读

1,获取表数据01

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
  5. print_r($row);echo "
    \n";
  6. }
  7. ?>

输出结果: php获取表数据

2,获取表数据02

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. while($row=mysql_fetch_object($result)){
  5. echo "ID:" .$row->id." text: ".$row->name."
    \n";
  6. }
  7. ?>

输出结果: php获取表数据

3,获取表数据03

  1. mysql_connect("localhost","dev","mysql");
  2. mysql_select_db("dev");
  3. $result=mysql_query("select id,name from tb_test");
  4. print "";
  5. print "
  6. ";
  7. print "
  8. ";
  9. while ($row = mysql_fetch_array($result))
  10. {
  11. print "
  12. ";
  13. print "
  14. ";
  15. print "
  16. ";
  17. }
  18. print "
  19. 编码名字
    {$row["id"]}{$row["name"]}
    ";
  20. ?>

输出结果: php获取表数据 4,获取数据分页04

  1. $page=isset($_GET['page'])?intval($_GET['page']):1;
  2. $num=3; //每页显示3条数据
  3. $db=mysql_connect("localhost","dev","mysql");
  4. $select=mysql_select_db("dev",$db);
  5. $total=mysql_num_rows(mysql_query("select id,name from tb_test"));
  6. $pagenum=ceil($total/$num);
  7. If($page>$pagenum || $page == 0){
  8. Echo "Error : Can Not Found The page .";
  9. Exit;
  10. }
  11. $offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
  12. $info=mysql_query("select id,name from tb_test limit $offset,$num");
  13. print "";
  14. print "
  15. ";
  16. print "
  17. ";
  18. While($row=mysql_fetch_array($info)){
  19. print "
  20. ";
  21. print "
  22. ";
  23. print "
  24. ";
  25. print "
  26. ";
  27. }//显示数据
  28. print "
  29. 编码名字
    {$row["id"]}{$row["name"]}
    ";
  30. For($i=1;$i<=$pagenum;$i++){
  31. $show=($i!=$page)?"$i":"$i";
  32. Echo $show." ";
  33. }
  34. ?>

输出结果: php获取表数据

人气教程排行