建立数据库:
mysql -u root -p
show databaese;
create databaes edu;
create databases edu;
use edu;
create table user (uname varchar(10),age int(10));
insert into user (uname,age) values ('rlm',22);
PHP代码段:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<style>
body{ text-align:center}
.bg_bs{ background-color:#9C3; color:#000;}
.bg_hs{ background-color:#CCC; color:#FFF;}
</style>
<?php
$mysqli = new mysqli ('localhost','root','111','edu');//连接数据库
$mysqli -> query("SET NAMES UTF-8");//设置显示的编码语言
/*$i = 1;
while($i<=20){
$sql = "insert into user (uname,age) values ('stu_{$i}',{$i})";
$result = $mysqli -> query($sql);
$i++;
}
*/
$sql = "select * from user ";//查询语句
$result = $mysqli -> query($sql);//返回集
echo "<table>";
while ($row = $result->fetch_assoc()){//循环输出
if ($row['age']<=10){
echo "<tr class='bg_hs'>";
}else{
echo "<tr class='bg_bs'>";
}
echo "<td>学员名:".$row['uname']."</td><td>年龄:".$row['age']."</td>";//输出学员名
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
没有执行批量添加mysql语句
批量增加之后: