记住启动的端口号(通常是3306)
现在开始php的mysqli之旅
首先当然是创建数据库
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
创建成功之后
可以进入mysql查看是否已经创建
如果配置好的话
到cmd中输入mysql -uroot -p
如果mysql没添加到环境变量的话
可以到mysql的安装目录的bin目录里
右键+shift键
然后点击此处用命令窗口打开
然后再输入上面那条命令
然后输入密码
然后可以输入命令
show databases;(别忘了分号)
如果成功创建的话会发现有myDB这个数据库
不熟悉的sql命令的同学可先点开下面的链接去看看常用的sql命令
http://blog.csdn.net/jenny0107/article/details/1332815
创建数据库之后就是创建表
因为我们已经创建了数据库,所以连接时把数据库的名字加上
全部代码改为
首先输入use myDB;
然后输入show tables;
这样就可以看到有表myGuests
创建表之后就是插入数据了
将代码换为
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";
if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>
如果有很多条插入的话,使用预处理加绑定会比较清晰而且方便
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES(?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);
// 设置参数并执行
$firstname = "John";
$lastname = "Doe";
$email = "[email protected]";
$stmt->execute();
$firstname = "Mary";
$lastname = "Moe";
$email = "[email protected]";
$stmt->execute();
$firstname = "Julie";
$lastname = "Dooley";
$email = "[email protected]";
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
如果想在mysql里查看数据的话
在mysql命令模式里(已经进入myDB这个数据库)
输入select * from myGuests;
这样就可以显示myGuests表里的全部数据了
然后就是获取表里的数据
将代码换为
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出每行数据
while($row = $result->fetch_assoc()) {
echo "
id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"];
}
} else {
echo "0 results";
}
$conn->close();
?>
其实查询的核心语句是
select * from 数据表 where 字段名=字段值
可以将*换成想要读取的字段,*代表读取全部字段
我是建议直接用*的,等输出时再输出想要的字段值就是了
where后接的是条件,多个条件有and连接,注意空格
至于改的话
代码是
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET email='[email protected]' WHERE firstname='John' AND LastName='Doe'";
$result = $conn->query($sql);
$conn->close();
?>
删除的话
代码为
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM MyGuests WHERE LastName='Griffin'";
$result = $conn->query($sql);
$conn->close();
?>
$conn->close();