php 操作数据库



<?php
    /*本例是用PHP连接一个mysql数据库操作的演示,
实现连接打开一个库,并读取数据的基本功能。
数据库名称为:dbname 表名为:person
分别有7个字段:id userid sex age tel email address
服务器;数据库编码 均采用 utf-8
mysql_query("set names 'gbk'"); // //这就是指定数据库字符集,一般放在连接数据库后(解决数据库乱码)
    */
?>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<style type="text/css">
<!--
input { font-size:9pt;}
A:link {text-decoration: underline; font-size:9pt;color:000059}
A:visited {text-decoration: underline; font-size:9pt;color:000059}
A:active {text-decoration: none; font-size:9pt}
A:hover {text-decoration:underline;color:red}
body,table {font-size: 9pt}
tr,td{font-size:9pt}
-->
</style>
<title>注册会员列表 - 读取mysql的测试</title>
</HEAD>
<body alink="#FF0000" link="#000099" vlink="#CC6600" topmargin="8" leftmargin="0" bgColor="#FFFFFF">
<br><br><center><font color=green size=3><b>注 册 会 员 列 表</b></font></center>
<br>
<table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2">
<tr bgcolor="#6b8ba8" style="color:FFFFFF">
    <td width="8%" align="center" valign="bottom" height="19">ID</td>
    <td width="10%" align="center" valign="bottom">姓名</td>
    <td width="5%" align="center" valign="bottom">性别</td>
    <td width="8%" align="center" valign="bottom">年龄</td>
    <td width="20%" align="center" valign="bottom">联系电话</td>
    <td width="20%" align="center" valign="bottom">电子邮件</td>
    <td width="20%" align="center" valign="bottom">家庭住址</td>
</tr>


<?php
/*
   I D:<input type="text" name="id" value= "sz00001" /><br>
   姓名:<input type="text" name="username" value= "zhangsan" /><br>
   性别:<input type="text" name="sex" value= "女" /><br>
   年龄:<input type="text" name="age" value ="23" /><br>
   联系电话:<input type="text" name="tel" value="138 8888 8888" /><br>
   电子邮件:<input type="text" name="email" value="123@163.com" /><br>
   家庭住址:<input type="text" name="adress" value="深圳市南山区" /><br>
*/

  if(isset($_POST['submit'])){//检测到按下了提交
  @$id=$_POST['id'];
   @$username=$_POST['username'];
    @$sex=$_POST['sex'];
     @$age=$_POST['age'];
      @$tel=$_POST['tel'];
       @$email=$_POST['email'];
        @$adress=$_POST['adress'];
  
  $conn=@mysql_connect("localhost","root","")or die("链接错误");
  mysql_select_db("dbname",$conn);
     $sql="INSERT INTO person(id,username,sex,age,tel,email,adress) values
     ('$id','$username','$sex','$age','$tel','$email','$adress')";
  if ( !mysql_query($sql,$conn)){
    echo "写入数据错误: " . mysql_error();
    }
}
 ?>
 
<?php
    //连接到本地mysql数据库
    $myconn=@mysql_connect("localhost","root","") or die("链接错误");
    //选择dbname为操作库
    //mysql_query("set names 'gbk'"); // //这就是指定数据库字符集,一般放在连接数据库后面就系了
    mysql_select_db("dbname",$myconn);
    $strSql="select * from person";
    //用mysql_query函数从user表里读取数据
    $result=mysql_query($strSql,$myconn);
 //list($username,$sex,$age,$tel,$email,$adress) = mysql_fetch_row($result);
    while($row=mysql_fetch_array($result))//通过循环读取数据内容
    {
?>
<tr>
    <td align="center" height="19"><?php echo $row["id"]?></td>
    <td align="center"><?php echo $row["username"]?></td>
    <td align="center"><?php echo $row["sex"]?></td>
    <td align="center"><?php echo $row["age"]?></td>
    <td align="center"><?php echo $row["tel"]?></td>
    <td align="center"><?php echo $row["email"]?></td>
    <td align="center"><?php echo $row["adress"]?></td>
</tr>
<?php
    }
    //关闭对数据库的连接
    mysql_close($myconn);
?>
</table>

 <br/> <br/>
<table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2">
<tr bgcolor="#6b8ba8" style="color:FFFFFF">
    <td width="8%" align="center" valign="bottom" height="19">ID</td>
    <td width="10%" align="center" valign="bottom">姓名</td>
    <td width="5%" align="center" valign="bottom">性别</td>
    <td width="8%" align="center" valign="bottom">年龄</td>
    <td width="20%" align="center" valign="bottom">联系电话</td>
    <td width="20%" align="center" valign="bottom">电子邮件</td>
    <td width="20%" align="center" valign="bottom">家庭住址</td>
</tr>
<?php
if (isset($_POST['search1'])) {
      $id = $_POST['id'];
   $conn=@mysql_connect("localhost","root","")or die("链接错误");
      mysql_select_db("dbname",$conn);
      $query = "SELECT * FROM person WHERE id='$id'";
      $result = mysql_query($query);
 if ($row = mysql_fetch_array($result)) {
  do {
   ?>
   <tr>
    <td align="center" height="19"><?php echo $row["id"]?></td>
    <td align="center"><?php echo $row["username"]?></td>
    <td align="center"><?php echo $row["sex"]?></td>
    <td align="center"><?php echo $row["age"]?></td>
    <td align="center"><?php echo $row["tel"]?></td>
    <td align="center"><?php echo $row["email"]?></td>
    <td align="center"><?php echo $row["adress"]?></td>
   </tr>
   <?php
  } while($row = mysql_fetch_array($result));
  } else {print " 对不起,再我们的数据库中,没有找到符合的纪录。 ";}
}
?>
<?php
if(isset($_POST['search'])){//检测到按下了提交
 @$id=$_POST['id'];
  @$username=$_POST['username'];
   @$sex=$_POST['sex'];
    @$age=$_POST['age'];
     @$tel=$_POST['tel'];
      @$email=$_POST['email'];
       @$adress=$_POST['adress'];
 $conn=@mysql_connect("localhost","root","")or die("链接错误");
 mysql_select_db("dbname",$conn);
  if($id == ""){$id = '%';}
  if ($username == ""){$username = '%';}
  if($sex == ""){$sex = '%';}
  if($age == ""){$age = '%';}
  if($tel == ""){$tel = '%';}
  if($email == ""){$email = '%';}
  if($adress == ""){$adress = '%';}
  $result = mysql_query("SELECT * FROM person WHERE id LIKE '$id%' AND username LIKE '$username%'
   AND sex LIKE '$sex%' AND age LIKE '$age%' AND tel LIKE '$tel%'
   AND email LIKE '$email%' AND adress LIKE '$adress%'",$conn);
  if ($row = mysql_fetch_array($result)) {
  do {
 ?> 
   <tr>
    <td align="center" height="19"><?php echo $row["id"]?></td>
    <td align="center"><?php echo $row["username"]?></td>
    <td align="center"><?php echo $row["sex"]?></td>
    <td align="center"><?php echo $row["age"]?></td>
    <td align="center"><?php echo $row["tel"]?></td>
    <td align="center"><?php echo $row["email"]?></td>
    <td align="center"><?php echo $row["adress"]?></td>
   </tr>
 <?php
  } while($row = mysql_fetch_array($result));
  } else {print " 对不起,再我们的数据库中,没有找到符合的纪录。 ";}
 }
 
 ?>
 
</table>

<style>
 .outer{ border-style:solid;
         solid #00F;
   width :370px;
   height :150px;
   margin-left:400px;
  }
 .same{
  margin-top:10px;
  margin-left:10px;
 }
  
</style>

 <br/> <br/> <br/>
 <div class ="outer" align= "center" >
<form   action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <div class ="same" >
 I&nbsp;&nbsp;&nbsp;&nbsp;D :<input class="id" type="text" name="id" size=25 maxlength=15><br/>
 姓名 :<input type="text" name="username"  size=25 maxlength=15><br/>
 性别 :<input type="text" name="sex"    size=25 maxlength=15><br/>
 年龄 :<input type="text" name="age"    size=25 maxlength=15><br/>
 电话 :<input type="text" name="tel"    size=25 maxlength=15><br/>
 邮箱 :<input type="text" name="email"  size=25 maxlength=15><br/>
 地址 :<input type="text" name="adress" size=25 maxlength=15><br/>
 </div>
<p>
<input type="submit" name="search"  value="查询">
</form>
</div>

</BODY>
</HTML>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值