PHP mysql_connect()连接-已淘汰

9 篇文章 2 订阅

1、首先在mysql命令控制台新建数据库

mysql> create database test;
Query OK, 1 row affected (0.04 sec)

mysql> use test;
Database changed

mysql> create table test_table(
    -> id int(5) auto_increment primary key,
    -> user char(10),
    -> pass char(10),
    -> text char(10),
    -> sex char(1),
    -> connect char(10));
Query OK, 0 rows affected (0.21 sec)

mysql>

2、登录注册代码

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <div>
        <b>登录</b>
        <form action="login.php" method="post">
            <p><span>用户名:</span><input type="text" name="user" required="" /></p>
            <p><span>密码:</span><input type="password" name="pass"/ required=""></p>
            <p><input type="reset" name=""/> <input type="submit" name=""/></p>
        </form>
        <a href="register.html">新用户注册</a>
    </div>
</body>
</html>

register.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
    <div>
        <b>注册</b>
        <form action="register.php" method="post">
            <p><span>用户名:</span><input type="text" name="user" required=""/></p>
            <p><span>密码:</span><input type="password" name="pass" required=""/></p>
            <p><span>性别:</span>
            <input type="radio" name="sex" checked="checked" value="男"/>男
            <input type="radio"  name="sex" value="女"/>女
            </p>
            <p><span>联系:</span><input type="text" name="connect" value="" required=""/></p>
            <p><span>文本:</span><input type="text" name="text" required=""/></p>
            <p><input type="reset" name=""/> <input type="submit" name=""/></p>
        </form>
        <a href="index.html">已注册</a>
    </div>
</body>
</html>

login.php

<meta charset="utf-8">
<?php
    header ( "Content-type:text/html;charset=utf-8" );
    // Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
    // error_reporting(0); // 错误警告不显示

    $conn = mysql_connect('localhost','root','') or die("error connecting") ;
    mysql_query("set names 'utf8'");  
    mysql_select_db("test"); 

    $user = $_POST['user'];
    $pass = $_POST['pass'];

    $sql="SELECT * FROM test_table where user='$user' and pass='$pass'"; 
    $rs = mysql_query($sql,$conn);
    $row = mysql_fetch_array($rs); 
    // var_dump($row);

    if($row['user']==$user&& $row['pass']==$pass){
        echo $row['user']."登陆成功!";
    }
    else{
        echo"登录失败,请重新登录!";
    }
?>

register.php

<meta charset="utf-8">
<?php
    header ( "Content-type:text/html;charset=utf-8" );
    // Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
    error_reporting(0); // 错误警告不显示

    $conn = mysql_connect('localhost','root','') or die("error connecting") ;
    mysql_query("set names 'utf8'");  
    mysql_select_db("test"); 

    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $sex = $_POST['sex'];
    $connect = $_POST['connect'];
    $text = $_POST['text'];

    $sql = "INSERT INTO test_table VALUES ('' ,'{$user}' ,'{$pass}','{$text}','{$sex}' ,'{$connect}' )";
    $result = mysql_query($sql,$conn);

    echo("注册成功!!!<br/><a href='index.html'>点击登录</a>");

?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值