加入收藏 | 设为首页 | 会员中心 | 我要投稿 鞍山站长网 (https://www.0412zz.cn/)- 智能营销、数据计算、数据可视化、负载均衡、研发安全!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

实例讲解php数据访问

发布时间:2022-07-15 12:49:41 所属栏目:PHP教程 来源:互联网
导读:这篇文章主要以实例讲解的方式为大家详细介绍了php数据访问,数据访问有两种方式,本文为大家揭晓,感兴趣的小伙伴们可以参考一下。 本文实例为大家分享了两种php数据访问方式,大家可以进行比较,分析两种方式的异同,最后为大家提供了一个小练习,具体内容
    这篇文章主要以实例讲解的方式为大家详细介绍了php数据访问,数据访问有两种方式,本文为大家揭晓,感兴趣的小伙伴们可以参考一下。
 
    本文实例为大家分享了两种php数据访问方式,大家可以进行比较,分析两种方式的异同,最后为大家提供了一个小练习,具体内容如下。
 
    方式一:已过时,只做了解
 
    1.造一个连接(建立通道)
 
    $db=mysql_connect("localhost","root","123");     //括号内是“服务器地址”,“用户名”,“密码”
 
    2.选择操作哪个数据库
 
    mysql_select_db("mydb","$db");
 
    3.写sql语句
 
    $sql="select * from Info";
 
    4.执行sql语句
 
    $result=mysql_query($sql);      //query 有查询之意
 
    5.从结果集($result)中取数据
 
    $row=mysql_fetch_row($result);  //每执行一次读取一行数据
    
    $row1=mysql_fentch_row($result);  //执行第二条数据
    
    var_dump($row);
    
    //读取全部数据用循环:
    
    while($row=mysql_fetch_row($result))
    
    {
    
      var_dump($row);  
    
    }
    方法二:面向对象
 
    1.造一个连接对象:
 
    $db=new MySQLi("localhost","root","123","mydb")  //括号内的内容依次为“服务器地址”,“用户名”,“密码”,“数据库名称”
 
    2.判断连接是否出错:
 
    2.1 mysqli_connect_error();  //代表连接出错
 
    2.2
 
    if(mysqli_connect_erroe())
    
           {
    
        echo "连接失败!";
    
        exit();  //退出程序
    
            }
    2.3 !mysqli_connect_error or die ("连接失败!"); //“or”前面代表连接正确,后面代表连接失败
 
    3. 写sql语句:
 
    $sql="select * from nation";
 
    4. 执行sql语句:如果执行成功返回结果集对象,如果执行失败返回false
 
    $result=$db->query($sql);
 
    5.从结果集中读取数据,先判断是否有数据
 
    if($result)
    
    {
    
      //返回一行数据的索引数组,每次执行返回一条数据
    
       var_dump($result->fetch_row());  
    
      while($row=$result->fetch_row)
    
      {
    
        var_dump($row);
    
      }
    
      //返回一行数据的关联数组,每次执行返回一条数据
    
      var_dump($result->fetch_row());  
    
      //通过二维数组返回所有数据
    
      var_dump($result->fetch_all());
    
      //以对象的方式返回一行数据
    
      var_dump($result->fetch_object());
    
    }
    练习:
 
    1.以下拉菜单的形式在页面显示nation表
 
    $db=new MySQLi("localhost","root","","mydb");
    
    !mysqli_connection_erroe() or die ("连接失败!");
    
    $sql="select*from nation";
    
    $result=$db->query($sql);
    
    if($result)
    
    {
    
      $att=$result->fetch_all();
    
      echo "<select>";
    
      foreach ($att as $value)
    
      {
    
        echo "<option value='{$value[0]}'>{$value[1]}</option>";
    
      }
    
      echo "</select>";
    
    }
    2. 把Info表查出来,以表格的形式显示
 
    $db=new MySQLi("localhost","root","","mydb");
    
    !mysqli_connecton_error() or die("连接失败!");
    
    $sql="select * from info";
    
    $result=$bd->query($sql);
    
    if($result)
    
    {
    
    $att=$result->fetch_all();
    
    echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
    
    echo "<tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";
    
    foreach ($att as $value)
    
    {
    
     echo "<tr>
    
    <td>{$value[0]}</td>
    
    <td>{$value[1]}</td>
    
    <td>{$value[2]}</td>
    
    <td>{$value[3]}</td>
    
    <td>{$value[4]}</td>
    
    </tr>";
    
    }
    
    echo "</table>";
    
    }
    
       
    
    //也可以用for循环
    
    if($result)
    {
      $arr=$result->fetch_all();
      echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
      echo "<tr><td>Code</td><td>Name</td><td>Sex</td><td>Nation</td><td>Birthday</td></tr>";
      for($i=0;$i<count($arr);$i++)
      {
        echo "<tr>
        <td>{$arr[$i][0]}</td>
        <td>{$arr[$i][1]}</td>
        <td>{$arr[$i][2]}</td>
        <td>{$arr[$i][3]}</td>
        <td>{$arr[$i][4]}</td>  
        </tr>";
      }
      echo "</table>";
    }
    以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

(编辑:鞍山站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读