第十二篇 PHP 和 MySql

141 阅读4分钟

十二 PHP 和 MySql

一 PHP 基本介绍
1.由于php属于后端语言,对语法要求特别严格.每句代码结束必须添加分号;
2.文件名不能有中文
3.文件路径不能有中文
4.文件必须使用服务器(localhost 或者127.0.0.1)打开
5.http 默认端口是80
6.https 默认端口号是443

端口:
	就是文件夹
	服务器  -- 就是一台电脑 --只有一个分区
	
	有256个文件夹  编号 0-255
	在这个文件夹里面 能都存在 256 个文件夹 0 - 255
	端口号  0-65535


json_encode($arr)    将关联数组转成 json字符串
json_decode(str)     将一个json字符串转成 关联数组
二 基本语法
基本语法:
<?php
//输出的代码
?>


php里面的输出语句:
	echo '输出'; 字符串,数字等简单的数据类型
	print_r(输出); 输出数组等复杂类型
	var_dump(数组);
	
<?php
  echo '1<br>';
  print_r("bbbbbb<br>");
  print_r('cccc<br>');

?>
     
 <?php
    hearder('content-type:text/html;charset=utf-8');
	echo '你好';
    
  ?>
三 PHP的变量
1.php定义变量
    $a = 10;
    echo = 10;//10

2.拼接:点.
    $ name = '你好';
    echo '我叫'.$name;


3.php监测数据类型 gettype()
    interger 整数型
    double 浮点型
    boolean 布尔型
    string 字符串
    null
    array  数组
    object  php类的数据类型

    $a = 10;
    echo gettype($a);

    $a;
    echo gettype($a);//null

    $a = array();
    echo gettype($a);//array

    class aa{

    }
    $a = new aa();
    echo gettype($a);//object
四PHP基本结构
1.php 分支  if elseswitch

        $ a = 10;
        if( $a < 10){
            echo '小于10';
        }else{
            echo '大于10';
        }


         switch($a){
            case 111:
              echo '等于111'
              break;
         }
 
2. php 循环
 for{$i = 0; $i < 10;$i++}{
 	echo '$i';
 }
 
 for()
 	
//关联数组:(数组里面是逗号)
	$arr = array(
		'name' =>'11',
		'age' =>'20',
	);
//关联数组的创建
foreach($arr as $key => $val){
	echo $key.'<br>';//键
	echo $val.'<br>';//值
}

//函数的调用
function fn($a){
	echo $a;
};
fn($a);

五 PHP数组
php数组:
    索引数组
        $arr = [1,2,3,4,5,6];
        $arr = array(1,2,3,4,5,6,7);
        $arr = array('aa','BBB');

    关联数组
        $arr = array(
            'name' => 'ws',
            'sex' => 'n',
            'age' => '10'
        );

    数组的长度   count(数组))
    二维数组
         $arr = array(
            array(
                'name' => '1'
            ),
            array(
                'name' => '2'
            )
        );
        print_r($arr);
PHP:
json_encode($arr)    将关联数组转成 json字符串
json_decode(str)     将一个json字符串转成 关联数组 

   $arr = array(
        'name' => 'ttt',
        'age' => '20',
        'sex' => 'n'
    );
    print_r(json_encode($arr));



    $str = '{"name":"ttt","age":"20","sex":"n"}';
    print_r(json_decode($str));
Document
Document
六 post 和 get 的区别
 $_POST[] 前端传输使用post
 $_GET[]  前端传输使用get
 $_REQUEST[]  不清楚前端使用哪种传输方式 
   form 中的获取数据的两种方式post 和 get的区别
   1.get是从服务器上获取数据,post是从服务器传送数据
   2.get是把参数数据队列加到提交表单的action属性所指的url中.值和表单内各个字段一一对应,在url中可以看到post是通过http post机制,将表单内各个字段与其内容放置在HTML header内以前传送到action 属性所指的url 地址.用户看不到这个过程
   3.对于get方式.服务端用Request.QueryString 获取变量 的值.对于post凡是,服务端用Request.Form 获取提交的数据.
   4.get传送的数据量小,不能大于2kb.post的传送的数据量较大,一般默认不受限制.
   5.get安全性非常低.post安全性较高.但是执行效率却比post方法号

  建议:
1.get方式的安全性较post 方式差一些.包含机密信息,建议用post数据提交方式
2.在做数据查询是,建议用get,而在做数据添加,修改或删除是,建议用post方法
    
    
XSS脚本攻击:
	解决1:正则表达式
     解决2:服务器解决脚本攻击  strip_tags();
七 PHP做数据渲染
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .box{
            width:1000px;
            overflow:hidden;
            margin:0 auto;
        }
        .box div{
            width:178px;
            border:1px solid red;
            margin:10px;
            float:left;
        }
        .box h4{
            text-align:center;
            font-size:24px;
        }
        .box h5{
            font-size:18px;
        }
        .box h6{
            font-size:14px;
        }
        .box img{
            display:block;
            margin:0 auto;
            width:80%;
        }
    </style>
</head>
<body>
    <div class="box">
        <!-- <div>
            <img src="">
            <h4></h4>
            <h5></h5>
            <h6></h6>
        </div> -->
        <?php
            include './bookdata.php';  //引入外部php文件

            for($i = 0 ; $i < count($data) ; $i++){
                echo "<div>
                        <img src='{$data[$i]['img']}'>
                        <h4>{$data[$i]['bookName']}</h4>
                        <h5>{$data[$i]['author']}</h5>
                        <h6>{$data[$i]['status']}</h6>
                    </div>";
            }
        ?>

        <!-- 注: echo 打印标签的时候  如果标签里面有引号:  echo "  里面用单引号  " -->
    </div>
</body>
</html>
<?php
    $data = array(
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3482279926,1815985476&fm=58&s=9784F104C2B045863B80E1C30300A09F',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3022993731,997538939&fm=58&s=5FB405C780459CF69605CEBF0300B00B',
            'bookName' => '  斗破苍穹 ',
            'author' => '作者:天蚕土豆',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3871287336,1884349563&fm=58&s=8EE441831C8B80D4CDC934BF03009000',
            'bookName' => ' 造化之门 ',
            'author' => ' 重生之神级败家子 ',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3143715676,542732954&fm=58&s=3C374497428848FC3BBDB0EA0300501C',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2433004558,2546252357&fm=58&s=EF97C70015C34CE80A046AF903005026',
            'bookName' => ' 终极教师 ',
            'author' => '作者:鹅',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3871287336,1884349563&fm=58&s=8EE441831C8B80D4CDC934BF03009000',
            'bookName' => ' 造化之门 ',
            'author' => ' 重生之神级败家子 ',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3143715676,542732954&fm=58&s=3C374497428848FC3BBDB0EA0300501C',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2433004558,2546252357&fm=58&s=EF97C70015C34CE80A046AF903005026',
            'bookName' => ' 终极教师 ',
            'author' => '作者:鹅',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3871287336,1884349563&fm=58&s=8EE441831C8B80D4CDC934BF03009000',
            'bookName' => ' 造化之门 ',
            'author' => ' 重生之神级败家子 ',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3143715676,542732954&fm=58&s=3C374497428848FC3BBDB0EA0300501C',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2433004558,2546252357&fm=58&s=EF97C70015C34CE80A046AF903005026',
            'bookName' => ' 终极教师 ',
            'author' => '作者:鹅',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3871287336,1884349563&fm=58&s=8EE441831C8B80D4CDC934BF03009000',
            'bookName' => ' 造化之门 ',
            'author' => ' 重生之神级败家子 ',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3143715676,542732954&fm=58&s=3C374497428848FC3BBDB0EA0300501C',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2433004558,2546252357&fm=58&s=EF97C70015C34CE80A046AF903005026',
            'bookName' => ' 终极教师 ',
            'author' => '作者:鹅',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3871287336,1884349563&fm=58&s=8EE441831C8B80D4CDC934BF03009000',
            'bookName' => ' 造化之门 ',
            'author' => ' 重生之神级败家子 ',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=3143715676,542732954&fm=58&s=3C374497428848FC3BBDB0EA0300501C',
            'bookName' => ' 造化之门 ',
            'author' => '作者:鹅是老五',
            'status' => '连载...'
        ),
        array(
            "img" => 'https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2433004558,2546252357&fm=58&s=EF97C70015C34CE80A046AF903005026',
            'bookName' => ' 终极教师 ',
            'author' => '作者:鹅',
            'status' => '连载...'
        ),
    )
?>
八 注册页面
登录注册
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="./form.php" method="get">
        <p>
            <label>用户名</label>
            <input type="text" name="userN">
        </p>
        <p>
            <label>密 码</label>
            <input type="text" name="userP">
        </p>
        <P>
            <input type="submit" value="提交">
        </P>
    </form>
</body>
</html>


    /*
        $_POST[] 前端传输使用post
        $_GET[]  前端传输使用get
        $_REQUEST[]  不清楚前端使用哪种传输方式
    */
?
用户注册
<?php
    header("content-type:text/html;charset=utf-8");

    $userName = $_REQUEST['userN'];
    $userPwd = $_REQUEST['userP'];

    //模拟用户数据
    $data = array(
        array(
            'name' => 'aaa',
            'password' => '111'
        ),
        array(
            'name' => 'bbb',
            'password' => '222'
        ),
        array(
            'name' => 'ccc',
            'password' => '333'
        ),
        array(
            'name' => 'ddd',
            'password' => '444'
        ),
        array(
            'name' => 'eee',
            'password' => '555'
        ),
        array(
            'name' => 'fff',
            'password' => '666'
        )
    );

    //信息比对
    
    $flag = true;
    for($i = 0 ; $i < count($data) ; $i++){
        if($data[$i]['name'] == $userName && $data[$i]['password'] == $userPwd){
            echo "<script>location.href='https://www.baidu.com'</script>";
            $flag = false;//修改口令
        }
    };
    //登陆失败
    if($flag){
        echo "<script>alert('请重新登陆')</script>";
        echo "<script>location.href='http://127.0.0.1/h5-1920/day21/land.html'</script>";
    }

?>
九.mysql 语句
增:
insert int '表名称' ('字段1','字段2','字段3') values ('value1','value2','value3');
删:
delect from '表名称' where 字段=val;
改:
update '表名称' set 要修改的字段 = 修改的值 where id = 2;
查:
查找所有数据:
select * from '数据表';
查找某一条数据:
select * from '数据表' where 筛选条件 id = 1;
查找指定内容:
select 字段1,字段2,...from '表名称' where id = 1;
查找一个范围:
select * from '表名称' where age < 10;
模糊匹配:
select * from '表名称' where 字段1 like '%查找的字符%'