PDO:php
<?php
/*$conn = mysql_connect('localhost','root','root');
mysql_select_db('db_shcool');
$result = mysql_query ('select*from stu2');*/
//print_r(mysql_fetch_array($result));
//print_r(mysql_fetch_array($result));
//mysql_close($conn);
/*while ($row = mysql_fetch_array($result)){
print_r($row);*/
/*$row=mysql_fetch_array($result);
while($row){
print_r($row);
$row=mysql_fetch_array($result);
}*/
$dsn = 'mysql:host=localhost;dbname=db_shcool';
$opt = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8');
$user = 'root';
$pwd = 'root';
$pdo = new PDO ($dsn, $user, $pwd,$opt);
$rs = $pdo -> query ("select * from result ");
while ($row = $rs -> fetch ()){.
print_r($row);
}
$pdo = null;
?> html
數據庫連接 註冊 登陸mysql
註冊 登陸 總頁面:sql
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<a href="html/reg.html">註冊</a>
<a href="html/login.html">登陸</a>
</body>
</html>
連接註冊頁面 註冊頁面內要有數據庫超連接 註冊頁面內填寫信息:數據庫
<!DOCTYPE html>
<html>post
<head>
<meta charset="UTF-8">
<title>註冊</title>
</head>fetch
<body>
<form action="../action/reg.php" method="post">
用戶名:<input type="text" name="name" /><br> 密碼: <input type="password" name="pwd" /><br> 確認密碼:
<input type="password" name="rpwd" /><br> 聯繫電話: <input type="text" name="tel" /><br>
<input type="submit" value="註冊" />
</form>
</body>url
</html>orm
數據庫鏈接:htm
<?php
include_once '../libs/fn_comm.php';
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$rpwd = $_POST['rpwd'];
$tel = $_POST['tel'];
refresh();
if($pwd == $rpwd){
$dsn = 'mysql:host=localhost;dbname=z_lign';
$opt = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8');
$pdo = new pdo ($dsn, "root","root",$opt);
$pwd = md5($pwd);
$sql = "INSERT INTO account(`name`,`pwd`,`tel`) VALUES('".$name."','".$pwd."','".$tel."')";
$rs = $pdo -> exec ($sql);
if($rs == 1){
echo '<h1>註冊成功</h1>';
}
} else {
echo '<h1>密碼不一致</h1>';
}
?>
數據庫鏈接內有自動跳轉頁面:
<?php
function refresh ($sec = 2, $url = 'http://localhost/test'){
header ('content-type: text/html;charset=utf8');
header('refresh:' . $sec . ';' . $url);
}
?>
登陸頁面 頁面內有數據庫連接 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陸</title>
</head>
<body>
<form action="../action/login.php" method="post">
用戶名:<input type="text" name="name" /><br>
密碼: <input type="password" name="pwd" /><br>
<input type="submit" value="登陸" />
</form>
</body>
</html>
數據庫鏈接:
<?php
include_once '../libs/fn_comm.php';
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$dsn = 'mysql:host=localhost;dbname=z_lign';
$opt = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8');
$pdo = new pdo ($dsn, "root","root",$opt);
$pwd = md5($pwd);
$sql = "select * from account where `name`='".$name."'and `pwd` ='".$pwd."'";
$rs = $pdo -> query ($sql);
if($rs-> fetch()){
echo '<h1> 登陸成功!</h1>';
} else {
echo '<h1> 登陸失敗!</h1>';
}
?>
註冊登陸表完成 !