第1关:PHP对MySQL的基本操作
<?php
/**
* 初始化数据库连接
*/
require 'public_function.php';
//创建对象,连接数据库
/*****begin*********/
$link=new mysqli('127.0.0.1','root','123123');
/*****end*********/
//判断数据库连接是否成功,如果不成功则显示错误信息并终止脚本继续执行
if($link->connect_error){
die('连接数据库失败!'.$link->connect_error);
}
//设置字符集,选择数据库
$link->query("drop database if exists 'educoder';");
$link->query("create database educoder;");
$link->query('set names utf8;');
$link->query('use educoder;');
//创建部门信息表
/*****begin*********/
$sql="create table emp_dept(
d_id int unsigned primary key auto_increment,d_name varchar(20) not null) charset=utf8;";
$link->query($sql);
/*****end*********/
//向部门信息表中插入数据
/*****begin*********/
$sql="INSERT INTO emp_dept VALUES
(1, '开发部'), (2, '媒体部'), (3, '人事部'),(4, '后勤部'),
(5, '市场部'), (6, '运维部'), (7, '销售部');";
$link->query($sql);
/*****end*********/
//修改用户表
/*****begin*********/
$sql="drop table if exists emp_info;";
$link->query($sql);
$sql="create table emp_info (
e_id int unsigned primary key auto_increment,
e_name varchar(20) not null,
d_id int unsigned not null,
date_of_birth timestamp not null,
date_of_entry timestamp not null
)charset=utf8;";
$link->query($sql);
/*****end*********/
//向其中添加用户数据
/*****begin*********/
$sql="INSERT INTO emp_info VALUES
(1, '小红', 1, '2015-4-9 17:51:00', '2015-4-9 17:52:00'),
(2, '李四', 5, '2008-4-3 13:33:00', '2013-10-24 17:53:00'),
(3, '王五', 4, '2008-4-3 13:33:00', '2015-4-21 13:33:00'),
(4, '赵六', 4, '2008-4-3 13:33:00', '2015-3-20 17:54:00'),
(5, '小兰', 2, '1989-5-4 17:33:00', '2012-6-18 17:54:00'),
(6, '小新', 5, '1993-9-18 17:36:00', '2015-2-28 17:36:00'),
(7, '小白', 2, '1991-10-17 17:37:00', '2014-8-16 17:37:00'),
(8, '小智', 7, '1987-6-20 17:37:00', '2015-1-10 17:38:00'),
(9, '大头', 6, &