准备工作
在XAMPP安装目录下,检查xampp\php\ext\php_mysql.dll文件是否存在,php_mysql.dll文件和php_mysqli.dll文件是PHP连接MySQL数据库的基础。
打开XAMPP软件的安装目录xampp\php\php.ini文件,检查以下语句是否存在,若不存在需添加:
extension=php_mysql.dll
extension=php_mysqli.dll启动XAMPP的apache和mysql。
附1:若没有建立数据库,使用以下语句:
CREATE DATABASE day1117 DEFAULT CHARACTER SET utf8;
附2:若没有简历数据表,使用以下语句:
CREATE TABLE myuser( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(30), password VARCHAR(30), age INT, email VARCHAR(30), addr VARCHAR(30) );
编写PHP脚本
完成PHP连接MySQL的步骤,PHP有关MySQL数据库的扩展
mysqli_connect()函数
参数有5个:
- host:指mysql数据库所在服务器的IP地址
- username:指登录mysql数据库的用户名
- passwd:登录mysql数据库的密码
- dbname:数据库名
- prot:端口号
返回值是一个mysqli_result类型的对象
附:mysqli_result类型的属性列表:
•mysqli_result — mysqli_result类
•mysqli_result::$current_field — Get current field offset of a result pointer
•mysqli_result::data_seek — Adjusts the result pointer to an arbitrary row in the result
•mysqli_result::fetch_all — Fetches all result rows as an associative array, a numeric array, or both
•mysqli_result::fetch_array — Fetch a result row as an associative, a numeric array, or both
•mysqli_result::fetch_assoc — Fetch a result row as an associative array
•mysqli_result::fetch_field_direct — Fetch meta-data for a single field
•mysqli_result::fetch_field — Returns the next field in the result set
•mysqli_result::fetch_fields — Returns an array of objects representing the fields in a result set
•mysqli_result::fetch_object — Returns the current row of a result set as an object
•mysqli_result::fetch_row — Get a result row as an enumerated array
•mysqli_result::$field_count — Get the number of fields in a result
•mysqli_result::field_seek — Set result pointer to a specified field offset
•mysqli_result::free — Frees the memory associated with a result
•mysqli_result::$lengths — Returns the lengths of the columns of the current row in the result set
•mysqli_result::$num_rows — Gets the number of rows in a result
过程化风格的连接步骤:
- 使用mysqli_connect()函数建立连接,该方法返回与数据库的连接对象,获取连接对象存入变量:
$conn=mysqli_connect('127.0.0.1','root','','myuser','3306');
var_dump($conn);
- 编写SQL语句字符串:
$sql="INSERT INTO myuser VALUES (NULL,'miejueshitai','12345',86,'miejue@qq.com',emei')";
使用mysqli_query()函数执行SQL语句:
$bool = mysqli_query($conn,$sql);
解析mysqli()函数返回的mysqli_result类型的对象;
关闭与MySQL数据库的连接,使用函数mysqli_close(连接对象)
mysqli_close($conn);
面向对象风格的连接步骤
(注意数据库和数据表的名称要与自己的相符)
/*
创建mysqli或者mysql对象,参见Mysql增强版本中的MySQLI类的构造器mysqli::__construct
*/
$mysqli= new mysqli('localhost','root','','myuser');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo 'Success... ' . $mysqli->host_info . "\n";
$sql="INSERT INTO myuser VALUES(NULL,'三丰道长','12345',86,'sanfeng@qq.com','武当')";
/*调用mysqli对象的query()方法,因为$mysqli对象中已经封装了连接信息,所以直接传sql语句作为参数即可
使用$mysql->query($sql)方法
query方法如果执行的是增删改操作,其返回值是一个布尔值;若是执行查询方法,其返回值是一个结果集对象*/
$mysqli->query('set names utf8');//设置编码
$mysqli->query($sql);
//关闭连接
$mysqli->close();
登录功能
废话不多说,直接上代码(注意数据库和数据表的名称要与自己的相符):
HTML页面(login.html):
<html>
<head>
<meta charset="utf-8">
<head>
<body>
<form id="myform" method="post" action="login.php">
<table>
<tr>
<td>用户名</td>
<td><input type="text" id="username" name="username"></input></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" id="pwd" name="pwd"></input></td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
PHP页面(login.php):
<?php
$username=$_POST['username'];
$pwd=$_POST['pwd'];
// echo $username.','.pwd;
$conn=mysqli_connect('127.0.0.1','root','','myuser','3306');
// var_dump($conn);
$sql="SELECT * FROM myuser WHERE name='"
.$username
."' AND pwd='"
.$pwd."'";
//echo $sql;
$result=mysqli_query($conn,$sql);
/*result结果集无论有无记录,只要sql语句执行成功,即返回非空对象
而在sql语句执行出错时就会返回false。所以我们不能简单地用$result对象存在与否来判断结果*/
if($result->num_rows > 0){
echo '登录成功';
}else{
echo '对不起,账户名密码不匹配';
}
?>
注册功能
HTML页面:
<html>
<head>
<meta charset="utf-8"/>
<title>注册</title>
</head>
<body>
<form action="register.php" method="POST">
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="pwd" name="pwd"></td>
</tr>
<tr>
<td><input type="submit"></td>
<!-- <td></td> -->
</tr>
</table>
</form>
</body>
</html>
PHP页面:
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$mysqli=new mysqli('localhost','root','','myuser');
if($mysqli->connect_error){
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$sql1="SELECT * FROM myuser where name='$username'";
$mysqli->query('set names utf8');
$result = $mysqli->query($sql1);
if($result->num_rows>0){
echo '这个名字已经存在,换个名字吧';
}else{
$sql2="INSERT INTO myuser(name,pwd) values('$username','$pwd')";
$rst=$mysqli->query($sql2);
if($rst){
echo '注册成功';
}else{
echo '注册失败';
}
}