PHP mysql数据库操作实例学习

发布时间:2019-08-08编辑:脚本学堂
本文介绍了php与数据库操作的几个例子,php编程中获取表数据的实例代码,有需要的朋友参考下。

1,获取表数据01
 

复制代码 代码示例:
<?php
  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 "<br/>n";
  }
?>

输出结果:
php获取表数据

2,获取表数据02
 

复制代码 代码示例:
<?php
  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."&nbsp;&nbsp;&nbsp;&nbsp;text: ".$row->name."<br/>n";
  }
?>

输出结果:
php获取表数据

3,获取表数据03
 

复制代码 代码示例:
<?php
  mysql_connect("localhost","dev","mysql");
  mysql_select_db("dev");
 
  $result=mysql_query("select id,name from tb_test");
  print "<table border='1'>";
  print "<th align = 'center'>编码</th>";
  print "<th align = 'center'>名字</th>";
 
  while ($row = mysql_fetch_array($result))
  {
  print "<tr>";
  print "<td align = 'center'>{$row["id"]}</td>";
  print "<td align = 'center'>{$row["name"]}</td>";
  }
  print "</table>";
?>

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

复制代码 代码示例:
<?php
$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 "<table border='1'>";
print "<th align = 'center'>编码</th>";
print "<th align = 'center'>名字</th>";
While($row=mysql_fetch_array($info)){
  print "<tr>";
  print "<tr>";
  print "<td align = 'center'>{$row["id"]}</td>";
  print "<td align = 'center'>{$row["name"]}</td>";
}//显示数据
print "</table>";
For($i=1;$i<=$pagenum;$i++){
       $show=($i!=$page)?"<a href='fenye.php?page=".$i."'>$i</a>":"<b>$i</b>";
       Echo $show." ";
}
?>

输出结果:
php获取表数据