• PHP数据访问


    方法1,过时方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <?php
        //1.生成连接
        $db_connect = mysql_connect("localhost","root","123456");//括号里分别是服务器 数据库用户名 密码
        
        //2.选择操作的数据库
        mysql_select_db("xinjian",$db_connect);//用后面那个链接操作前面那个数据库
        
        //3.写sql语句
        $sql = "select * from Info";
        
        //4.执行sql语句
        $result = mysql_query($sql);
        //var_dump($result);
        
        //5.处理查询结果
        $row = mysql_fetch_row($result);//执行一次 取一次数据
        
        print_r ($row);            //如果sql语句是增删改,返回的是影响的行数
        
        
    
    
    
    ?>
    
    </body>
    </html>

    方法2,面向对象的方式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    <?php
    //面向对象方式
        //1.生成链接对象
        $db=new MySQLi("localhost","root","123456","xinjian"); //括号里是服务器地址 root 用户名密码 数据库名
        
        //2.判断链接是否成功
        !mysqli_connect_error() or die("链接失败!"); 
        
        //die函数相当于把两句话合到一块
        /*echo "链接失败!";
        exit;    //直接退出程序,*/
        
        //3.写sql语句
        $sql = "select * from Info";
        
        //执行sql语句
        $result =$db->query($sql);
        
        //操作结果
        //$row = $result->fetch_row();
        //方法1.
        while($row=$result->fetch_row())
        {
            
            print_r($row);
            echo "<br>";
            
            }
            
        //方法2.    
        $attr = $result->fetch_all();//返回所有数据,以二维数组的形式返回
        print_r($attr);*/
    
        //方法3.
        $attr=$result->fetch_assoc();//返回一条数据,以关联数组的形式返回
        print_r($attr);
    
    ?>
    
    </body>
    </html>

    把表格显示在网页中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    
    <?php
    
        //造链接对象
        $db = new MySQLi("localhost","root","123456","xinjian");
        
        //判断是否出错
        !mysqli_connect_error() or die("链接失败");
        
        //写SQL语句
        $sql="select * from Info";
        
        //执行sql语句
        $result =$db->query($sql);
        
        //处b理查询数据并显示
        echo "<table width='100%' border='1' cellpadding='0' cellspacing='0'>";
        
        echo "<tr><td>代号</td><td>d姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";
        
        while($row=$result->fetch_row())
        {
            $sex=$row [2]?"男":"女";//三元运算判断,如果是ture返回男,如果是false,返回女
            
            $name=ShowNation($db,$row[3]);//改民族,调用下面的方法
            
            echo "<tr>";
            echo "<td>{$row[0]}</td><td>{$row[1]}</td><td>{$sex}</td><td>{$name}</td><td>{$row[4]}</td>";
            echo"</tr>";
            }
        
        echo "</table>";
        //改民族
        function ShowNation($db,$code)
        {    
            //写SQL语句
            $sql="select name from nation where code='{$code}'";
            
            //执行sql语句
            $result =$db->query($sql);
            $jieguo=$result->fetch_row();
            return $jieguo[0];
            }
    ?>
    </body>
    </html>
  • 相关阅读:
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    Unity碰撞检测
    Unity碰撞检测
    Unity碰撞检测
    Unity碰撞检测
    关于JavaScript中事件的一些重要说明
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/zhanghaozhe8462/p/5315292.html
Copyright © 2020-2023  润新知