Misty rain
php数据库操作-创建库和表以及插入数据
以上我们正确连接到了mysql数据库,本文将进一步创建数据库,表,在表中填充数据。
大家知道连接上数据库才能进行操作,同样的代码搬过来
<?php
/*
- 数据库操作*(创建数据库,表,插入数据,插入多条数据)
* - To change the template for this generated file go to
- Window - Preferences - PHPeclipse - PHP - Code Templates
*/
//先连接数据库
$servername="localhost";
$username="root";
$userpassword="";
$connent=new mysqli($servername,$username,$userpassword);
if($connent->connect_error){
die("连接失败: " . $connent->connect_error);
}else{
echo "连接成功";
}
//创建数据库
$createdatabase="create database testdb";
if($connent->query($createdatabase)==true){
echo "创建数据库成功";
}else{
echo "Error creating database: " . $connent->error;
}
?>
如果创建成功--》创建数据库成功
Error creating database: Can't create database 'testdb'; database exists 则证明已经存在
另外我们可以打开Navicat for MySql来查看
如果创建成功--》创建数据库成功
接下来跟着建表:
//创建表 原生的建表语句 id自增唯一 name age email
$createtable="create table zh(id int(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,name text,age int(3),email text)";
if($connent->query($createtable)==true){//执行
echo "创建表zh成功";
}else{
echo "Error creating table: " . $connent->error;
跟着插入数据,这里都是使用的原生语句。
//插入数据
$insertdata="insert into zh(name,age,email) values('zhanghao',23,'142*112@110.com')";
if($connent->query($insertdata)==true){
echo "插入数据成功";
}else{
echo "Error insert data: " . $connent->error;
}
//也可以如下这么写 也比较简单一些
/*if (mysqli_query($connent, $insertdata)) {
echo "插入数据成功";
} else {
echo "Error insert data: " . $connent->error;
}*/
//关闭数据库
mysqli_close($connent);
最后要关闭数据库,否则会一直添加 接下来多条我先把他注释
//插入多条数据
$insertdatas="insert into zh(name,age,email) values('test1',1,'1.com');";
$insertdatas .="insert into zh(name,age,email) values('tes2',2,'2.com');";
$insertdatas .="insert into zh(name,age,email) values('test3',3,'3.com')";
if ($connent->multi_query($insertdatas)==true) {
echo "插入多条数据成功";
} else {
echo "Error insert datas: " . $connent->error;
}
//关闭数据库
mysqli_close($connent);
完整代码片:
<?php
/*
- 数据库操作*(创建数据库,表,插入数据,插入多条数据)
* - To change the template for this generated file go to
- Window - Preferences - PHPeclipse - PHP - Code Templates
*/
//先连接数据库
$servername="localhost";
$username="root";
$userpassword="";
$dbname = "testdb";
$connent=new mysqli($servername,$username,$userpassword,$dbname);
if($connent->connect_error){
die("连接失败: " . $connent->connect_error);
}else{
echo "连接成功";
}
//创建数据库
$createdatabase="create database testdb";
if($connent->query($createdatabase)==true){
echo "创建数据库成功";
}else{
echo "Error creating database: " . $connent->error."<br>";
}
//创建表 原生的建表语句 id自增唯一 name age email
$createtable="create table zh(id int(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,name text,age int(3),email text)";
if($connent->query($createtable)==true){//执行
echo "创建表zh成功";
}else{
echo "Error creating table: " . $connent->error."<br>";
}
//插入数据
/$insertdata="insert into zh(name,age,email) values('zhanghao',23,'142112@110.com')";
if($connent->query($insertdata)==true){
echo "插入数据成功";
}else{
echo "Error insert data: " . $connent->error;
}*/
//也可以如下这么写 也比较简单一些
/*if (mysqli_query($connent, $insertdata)) {
echo "插入数据成功";
} else {
echo "Error insert data: " . $connent->error;
}*/
//插入多条数据
/*$insertdatas="insert into zh(name,age,email) values('test1',1,'1.com');";
$insertdatas .="insert into zh(name,age,email) values('tes2',2,'2.com');";
$insertdatas .="insert into zh(name,age,email) values('test3',3,'3.com')";
if ($connent->multi_query($insertdatas)==true) {
echo "插入多条数据成功";
} else {
echo "Error insert datas: " . $connent->error;
}*/
//关闭数据库
mysqli_close($connent);
?>
1
1
1
1
1
1
1%0abcc:009247.351-102945.351.f3006.19109.2@bxss.me
1
to@example.com>%0d%0abcc:009247.351-102946.351.f3006.19109.2@bxss.me
1
../../../../../../../../../../../../../../etc/passwd
../../../../../../../../../../../../../../windows/win.ini
1
1
1
1
1
1
1
response.write(9867658*9186886)
'+response.write(9867658*9186886)+'
1
1
1
1
1
1
1
1
1
1
1
1
1
1
{!{data:image/webp;base64,UklGRqQEAABXRUJQVlA4WAoAAAAwAAAA0AMAK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vtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ96QAD+/60eAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=}!}
555
1
555
555
555
555
1
1
1
555
555
1
1
1
555
1
555
1
555
1
1
1
1
1
1
1
1
1
1
1
1
1
1