一.环境
需要lamp用于测试
1.安装httpdyum -y install httpd``systemctl start httpd
2.安装数据库yum -y install mariadb mariadb-server``systemctl start mariadb
3.安装php相关yum install -y php php-devel php-fpm php-mysql php-common php-devel``systemctl start php-fpm
二.创建数据库
1.登陆数据库,默认安装的没密码mysql
2.创建one库create database one;
3.创建user表create table one.user(id int(11),name cahr(20),password char(20));
4.写入一个数据insert into one.user values(1,'abc','abcd');
三.代码
1.创建一个首页文件vim /var/www/html/index.html
<?php
$servername = "localhost"; #数据库地址
$dbusername = "root"; #登陆
$dbpassword = "";
$dbname = "one"; #登陆后切换到哪个库
$username = $_GET ['username']; #从get方法获取参数
$password = $_GET ['password'];
#登陆到数据库并建立连接
mysql_connect($servername,$dbusername,$dbpassword) or die ("数据库连接失败");
#查询语句,账号和密码从浏览器的get方法中获取
$sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
#切换到one库,并查询
$result = mysql_db_query($dbname, $sql);
#获取查询的状态,是否成功
$userinfo = mysql_fetch_array($result);
#判断一下,是否登陆成功
if (empty($userinfo))
{
echo "登录失败";
} else {
echo "登录成功";
}
#输出sql语句,用于查看是否将username和password传进去
echo "<p>SQL Query:$sql<p>";
?>
四.测验
浏览器输入,可以看到登陆成功了。xx=x&xx=x是用get方法传递参数。http://1.1.1.1/index.php?username=abc&password=abcd