1.sql查询
select表示要进行查询,'.'去出所有字段,FROM table 目标数据表,where数据过滤条件,ORDER BY排序条件,LIMIT 取出数量,常用于分页。
2.全部代码演示
gbook.php
<?php
$host = '127.0.0.1';
$dbuser = 'root';
$pwd = '';
$dbname = 'php10';
$db = new mysqli($host, $dbuser, $pwd, $dbname);
@ini_set('date.timezone','PRC');//将PHP的时区改为北京时间
//检查是否成功
if( $db->connect_errno <> 0){
echo"数据库连接失败";
echo $db->connect_error;
exit;
}
$db->query("SET NAMES UTF8");
$sql = "SELECT * FROM msg ORDER BY id DESC";
$mysqli_result = $db->query( $sql );
if($mysqli_result === false){
echo "sql错误";
exit;
}
$rows = [];
while( $row = $mysqli_result->fetch_array() ){
$rows[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'/>
<title>留言本</title>
<style>
.warp{
width:600px;
margin:0px auto;
}
.add{overflow: hidden;}
.add .content{
width:598px;60
margin:0px;
padding:0px;
}
.add .user{
float:left;
}
.add .btn{
float:right;
}
.msg{margin:20px 0px;background: #ccc; padding:5px;}
.msg .info{overflow: hidden;}
.msg .user{float:left;color:blue;}
.msg .time{float:right;color:#999;}
.msg .content{width:100%;}
</style>
</head>
<body>
<!--发表留言内容-->
<div class='warp'>
<div class='add'>
<form action='formsave.php' method='post'>
<textarea name= 'content' class='content' cols='50' rows='5'></textarea>
<input name='user' class='user' type='text'/>
<input class='btn' type='submit' value='发表留言'/>
</form>
</div>
<?php
foreach( $rows as $row ){
?>
<!--查看留言内容-->
<div class='msg'>
<div class='info'>
<span class='user'><?php echo $row['user'];?></span>
<span class='time'><?php echo date('Y-m-d H:i:s',$row['intime']);?></span>
</div>
<div class='content'>
<?php echo $row['content'];?>
</div>
</div>
<?php
}
?>
</div>
</body>
</html>
forsave.php
<?php
include('input.php');
include('connect.php');
$content = $_POST['content'];
$user = $_POST['user'];
$input = new input();
//调用函数,检查留言内容
$is = $input->post( $content );
if( $is == false ){
die('留言内容的数据不正确');
}
//调用函数,检查用户名
$is = $input->post( $user );
if( $is == false ){
die('用户名的数据不正确');
}
//var_dump($content,$user);
//第九天将留言写入数据库
$time = time();
$sql = "INSERT INTO msg (content, user, intime) values ('{$content}', '{$user}', '{$time}')";
$is = $db->query($sql);
//var_dump( $is );
header('location: gbook.php');
?>
input.php
<?php
class input{
//定义函数,对数据进行检查
function post($content){
if ($content == ''){
return false;
}
//禁止使用的用户名
$n = ["张三","李四","王五"];
foreach($n as $name){
if($content == $name){
return false;
}
}
return true;
}
}
?>
connect.php
<?php
//预先定义数据库连接参数
$host = '127.0.0.1';
$dbuser = 'root';
$pwd = '';
$dbname = 'php10';
//连接到数据库
$db = new mysqli($host, $dbuser, $pwd, $dbname);
//检查是否成功
if( $db->connect_errno <> 0){
echo'数据库连接失败';
echo $db->connect_error;
exit;
}
//设定数据库数据传输的编码
$db->query('SET NAMES UTF8');
//编写SQL
//$sql = 'insert into msg (content, user, intime) value ('1', 'user', 12345)';
$sql = "SELECT *FROM msg ORDER BY id DESC";
//执行SQL
$mysqli_result = $db->query( $sql );
if($mysqli_result === false){
echo 'SQL错误';
exit;
}
/*
首次调用显示最新的一条记录
重复调用,依次显示后面的记录
如果没有记录可以显示,就返回null
row = array(0,1,2) == true;
row = null ==fasle;
*/
$rows = [];
while( $row = $mysqli_result->fetch_array( MYSQLI_ASSOC ) ){
$rows[] = $row;
}
var_dump($rows);
?>