2020年5月7日
<?php
$link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
mysqli_set_charset($link,'utf-8');
//查询语句
$sql="SELECT * FROM books ORDER BY book_id DESC";
$res =mysqli_query($link,$sql);
var_dump($res);
$row=mysqli_fetch_row($res);
$row=mysqli_fetch_assoc($res);
$row=mysqli_fetch_object($res);
var_dump($row);
$data = [];
while($row=mysqli_fetch_assoc($res)){
$data[] =$row;
}
var_dump($data);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sql留言板</title>
<style>
.cb{
border: 1px solid blue;
}
</style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<div class="cb"><p>内容</p>
<div class="ca">
<b>姓名</b><sapn>邮箱</span>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加留言</title>
</head>
<body>
<h1>添加留言</h1>
<form action="./demo.php" method="post">
<p>昵称:<input type="text" name="name"></p>
<p>邮箱:<input type="text" name="email"></p>
<p>留言:<input type="text" name="comment"></p>
<input type="submit" value="添加">
</form>
</body>
</html>
<?php
$post =$_POST;
var_dump($post);
$link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'UTF-8');
$sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
$res=mysqli_query($link,$sql);
if($res && mysqli_afffected_rows($link)>0){
echo"成功";
}else{
echo"失败";
}
mysqli_close($link);
CREATE TABLE comment(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
content varchar(255) NOT NULL COMMENT '留言内容',
name varchar(10) NOT NULL COMMENT '姓名',
email varchar(60) NOT NULL COMMENT '邮箱'
);
//首页显示功能
<?php
$link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'utf8');
$sql= "SELECT * FROM comment";
$res=mysqli_query($link,$sql);
if($res ≡ false){
echo mysqli_errno($link),"<br>";
echo mysqli_error($link),"<br>";
exit('sql语句执行失败!');
}
$data =[];
while($row = mysqli_fetch_assoc($res)){
$data[]=$row;
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
<style></style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<?php foreach($data as $key → $liuyan){ ?>
<div class="cb">
<p><?php echo $liuyan['content'] ?></p>
<div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
<?php }?>
</body>
</html>
<?php
$link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
mysqli_set_charset($link,'utf-8');
//查询语句
$sql="SELECT * FROM books ORDER BY book_id DESC";
$res =mysqli_query($link,$sql);
var_dump($res);
$row=mysqli_fetch_row($res);
$row=mysqli_fetch_assoc($res);
$row=mysqli_fetch_object($res);
var_dump($row);
$data = [];
while($row=mysqli_fetch_assoc($res)){
$data[] =$row;
}
var_dump($data);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sql留言板</title>
<style>
.cb{
border: 1px solid blue;
}
</style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<div class="cb"><p>内容</p>
<div class="ca">
<b>姓名</b><sapn>邮箱</span>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加留言</title>
</head>
<body>
<h1>添加留言</h1>
<form action="./demo.php" method="post">
<p>昵称:<input type="text" name="name"></p>
<p>邮箱:<input type="text" name="email"></p>
<p>留言:<input type="text" name="comment"></p>
<input type="submit" value="添加">
</form>
</body>
</html>
<?php
$post =$_POST;
var_dump($post);
$link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'UTF-8');
$sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
$res=mysqli_query($link,$sql);
if($res && mysqli_afffected_rows($link)>0){
echo"成功";
}else{
echo"失败";
}
mysqli_close($link);
CREATE TABLE comment(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
content varchar(255) NOT NULL COMMENT '留言内容',
name varchar(10) NOT NULL COMMENT '姓名',
email varchar(60) NOT NULL COMMENT '邮箱'
);
//首页显示功能
<?php
$link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'utf8');
$sql= "SELECT * FROM comment";
$res=mysqli_query($link,$sql);
if($res ≡ false){
echo mysqli_errno($link),"<br>";
echo mysqli_error($link),"<br>";
exit('sql语句执行失败!');
}
$data =[];
while($row = mysqli_fetch_assoc($res)){
$data[]=$row;
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
<style></style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<?php foreach($data as $key → $liuyan){ ?>
<div class="cb">
<p><?php echo $liuyan['content'] ?></p>
<div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
<?php }?>
</body>
</html>
<?php
$link =mysqli_connect('127.0.0.1','root','root','class06')or exit('数据库连接失败');
mysqli_set_charset($link,'utf-8');
//查询语句
$sql="SELECT * FROM books ORDER BY book_id DESC";
$res =mysqli_query($link,$sql);
var_dump($res);
$row=mysqli_fetch_row($res);
$row=mysqli_fetch_assoc($res);
$row=mysqli_fetch_object($res);
var_dump($row);
$data = [];
while($row=mysqli_fetch_assoc($res)){
$data[] =$row;
}
var_dump($data);
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sql留言板</title>
<style>
.cb{
border: 1px solid blue;
}
</style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<div class="cb"><p>内容</p>
<div class="ca">
<b>姓名</b><sapn>邮箱</span>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加留言</title>
</head>
<body>
<h1>添加留言</h1>
<form action="./demo.php" method="post">
<p>昵称:<input type="text" name="name"></p>
<p>邮箱:<input type="text" name="email"></p>
<p>留言:<input type="text" name="comment"></p>
<input type="submit" value="添加">
</form>
</body>
</html>
<?php
$post =$_POST;
var_dump($post);
$link =mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'UTF-8');
$sql=sprintf("INSERT INTO COMMENT VALUES(DEFAULT, '%S','%S','%S')", $post['comment'],$post['name'],$post['email']);
$res=mysqli_query($link,$sql);
if($res && mysqli_afffected_rows($link)>0){
echo"成功";
}else{
echo"失败";
}
mysqli_close($link);
CREATE TABLE comment(
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
content varchar(255) NOT NULL COMMENT '留言内容',
name varchar(10) NOT NULL COMMENT '姓名',
email varchar(60) NOT NULL COMMENT '邮箱'
);
//首页显示功能
<?php
$link = mysqli_connect('127.0.0.1','root','root','liuyan')or exit('连接数据库失败');
mysqli_set_charset($link,'utf8');
$sql= "SELECT * FROM comment";
$res=mysqli_query($link,$sql);
if($res ≡ false){
echo mysqli_errno($link),"<br>";
echo mysqli_error($link),"<br>";
exit('sql语句执行失败!');
}
$data =[];
while($row = mysqli_fetch_assoc($res)){
$data[]=$row;
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
<style></style>
</head>
<body>
<h1>留言板</h1>
<p><a herf="./add.php">添加留言</a></p>
<?php foreach($data as $key → $liuyan){ ?>
<div class="cb">
<p><?php echo $liuyan['content'] ?></p>
<div class="ca"><b><?php echo $liuyan['name'] ?></b><span><?php echo $liuyan['email'] ?></span></div>
</div>
<div class="c-act">
<a herf="">编辑</a>
<a herf="">删除</a>
</div>
</div>
<?php }?>
</body>
</html>
php增删改查表
1.连接数据库选择数据库 并且判断数据库是否连接成功
2.设置字符集(编码)为了中文显示不乱码
3.准备sql语句
4.向mysql服务器发送sql语句执行
5.释放结果同时关闭数据库连接
查询的处理方式和新增 删除 修改的方式不同
查询的处理:
获取一个结果,这个函数的功能类似readdir();
相信为结果集合内有一个指针,每调用函数一次则将当前指针指向的内容拿出来
并且将指针指向下一个值的位置;同时如果获取不到值则返回false(NULL)
mysqli_fetch_array(结果集[,返回格式]);
返回格式:MYSQLI_BOTH(默认)/MYSQLI_NUM / MYSQLI_ASSOC
MYSQLI_BOTH 索引+关联
MYSQLI_NUM 索引
MYSQLI_ASSOC 关联
mysqli_fetch_row(结果集) 获取数据为索引数组
mysqli_fetch_assoc(结果集) 获取数据为关联数组
mysqli_fetch_object(结果集) 获取数据为对象格式
$row=mysqli_fetch_row($res);
$row=mysqli_fetch_assoc($res);
$row=mysqli_fetch_object($res);
var_dump($row);
获取错误信息:
mysql_errno($link); 获取错误代码(编号)
mysql_error($link);获取错误文字信息
mysql版留言板添加功能