代码练习
//bindColumn 把结果绑定到指定变量上
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root');
}catch(PDOException $e){
// 抛出错误,错误是你可以定义的
echo '数据库连接失败' . $e->getMessage();
}
$pre = $pdo -> prepare('SELECT * FROM `user`');
$exe = $pre -> execute();
// // 把mysql读取出来的这一列数据,放到$name变量中
$pre -> bindColumn('name',$name);
while ($pre->fetch(PDO::FETCH_ASSOC)){
echo '姓名:' . $name;
echo '<hr>';
}
//编码
header ('content-type:text/html;charset=utf-8');
//关闭pdo
$pdo = null;
unset($pdo);
//mysql统计条数 count
SELECT count(*) FROM `user`
// 6、字段重命名as,临时把字段名更改为想要的
SELECT `account` as a FROM `user`
//类
class Teacher{
public $name;
public $age;
// 构造方法(人事部)
public function __construct($n,$age){
// $this 代表 本类(Teacher)
$this->name = $n;
// $this->age ===等于 public $age;
$this->age = $age;
// 它可以访问 本类里的 成员属性和成员方法
$this->jh();
}
public function jh(){
return '我是php中文网的老师';
}
public function code(){
return '我会写代码';
}
// 析构方法(人事部),离职的时候执行的
// 所有代码执行完以后,才执行,可以用做日志记录
public function __destruct(){
echo '我离职了';
}
}
// new 类的时候,就把成员变量的值传进去
$ouyang = new Teacher('欧阳克','38');
echo $ouyang->name;
echo '<hr>';
echo $ouyang->age;
echo '<hr>';
// 在外部访问成员方法
echo $ouyang->code();
类预习
类成员
static 和重载技术
命名空间
pdo操作数据库类
- 列表
<?php
require_once 'fun.php';
$data = select('user','*');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户列表</title>
<link rel="stylesheet" href="list.css">
</head>
<body>
<div class="add"><a href="add.php">添加</a></div>
<table>
<thead>
<tr>
<th width="50">ID</th>
<th width="100">姓名</th>
<th width="100">年龄</th>
<th width="100">手机号</th>
<th width="200">注册时间</th>
<th width="200">登录时间</th>
<th width="100">状态</th>
<th width="100">修改 | 删除</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $v){
?>
<tr>
<td><?=$v['id'] ?></td>
<td><?=$v['name'] ?></td>
<td><?=$v['age'].'岁' ?></td>
<td><?=$v['tel'] ?></td>
<td><?=$v['ctime'] ?></td>
<td><?=date('Y-m-d H:i:s',$v['utime']) ?></td>
<td>
<?php
if($v['status'] == 1){
echo '<span style="color:green;">开启</span>';
}else{
echo '<span style="color:red;">关闭</span>';
}
?></td>
<td><a href="edit.php?id=<?=$v['id'] ?>">修改</a> | <a href="del.php?id=<?=$v['id'] ?>">删除</a></td>
</tr>
<!-- 这里要循环的html标签 -->
<?php
}
?>
</tbody>
</table>
</body>
</html>
- 添加
<?php
require_once 'fun.php';
if(!empty($_POST)){
$data=[
"name"=>$_POST['name'],
"age"=>$_POST['age'],
"tel"=>$_POST['tel'],
"pass"=>md5($_POST['pass']),
"ctime"=>date('Y-m-d H:i:s',time()),
"utime"=>0,
"status"=>1,
];
$addm=insert('user',$data);
if($addm =='true'){
echo '提交成功';
}else{
echo '提交失败';
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加用户</title>
<link rel="stylesheet" href="list.css">
</head>
<body>
<h2>添加用户</h2>
<form action="" method="post">
<div class="adds">
<div class="tli">
<label for="name">账号:</label>
<input type="text" id="name" name="name" placeholder="请输入用户名" required autofocus value=""/>
</div>
<div class="tli">
<label for="name">年龄:</label>
<input type="number" id="age" name="age" placeholder="请输入年龄" required value=""/>
</div>
<div class="tli">
<label for="name">手机:</label>
<input type="tel" id="tel" name="tel" placeholder="请输入手机号" required value=""/>
</div>
<div class="tli">
<label for="name">密码:</label>
<input type="password" id="pass" name="pass" placeholder="请输入密码" required value=""/>
</div>
<div class="tli">
<input type="submit" value="提交">
</div>
</div>
</form>
</body>
</html>
- 修改
<?php
$id= $_GET['id'];
require_once 'fun.php';
$data = find('user','*','id='.$id);
if(!empty($_POST)){
if(!empty($_POST['pass'])){
$datax=[
"name"=>$_POST['name'],
"age"=>$_POST['age'],
"tel"=>$_POST['tel'],
"pass"=>md5($_POST['pass']),
"ctime"=>date('Y-m-d H:i:s',time()),
"utime"=>0,
"status"=>1,
];}else{
$datax=[
"name"=>$_POST['name'],
"age"=>$_POST['age'],
"tel"=>$_POST['tel'],
"ctime"=>date('Y-m-d H:i:s',time()),
"utime"=>0,
"status"=>1,
];
}
$addm=update('user',$datax,'id='.$id);
if($addm =='true'){
echo '修改成功';
}else{
echo '修改失败';
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>修改用户</title>
<link rel="stylesheet" href="list.css">
</head>
<body>
<h2>添加用户</h2>
<form action="" method="post">
<div class="adds">
<div class="tli">
<label for="name">账号:</label>
<input type="text" id="name" name="name" placeholder="请输入用户名" required autofocus value="<?=$data['name']?>"/>
</div>
<div class="tli">
<label for="name">年龄:</label>
<input type="number" id="age" name="age" placeholder="请输入年龄" required value="<?=$data['age']?>"/>
</div>
<div class="tli">
<label for="name">手机:</label>
<input type="tel" id="tel" name="tel" placeholder="请输入手机号" required value="<?=$data['tel']?>"/>
</div>
<div class="tli">
<label for="name">密码:</label>
<input type="password" id="pass" name="pass" placeholder="留空则不修改" value=""/>
</div>
<div class="tli">
<input type="submit" value="提交">
</div>
</div>
</form>
</body>
</html>
- 删除
<?php
require_once 'fun.php';
$id= $_GET['id'];
$del=delete('user','id='.$id);
if($del){
echo '删除成功';
}else {echo '删除失败';
}
?>