php操作mysql语句

2019-05-07  本文已影响0人  TimeSHU

mysql语句

查看当前所有数据库
show databases;

查看当前所有表
show tables;

查看表结构
desc user;

创建数据库
create database msg;

创建数据库
create table use (
id int not null primary key auto_increment,
user varchar(20) not null,
pass varchar(100) not null
)charset utf8;

查看表所有内容
select * from user;

根据条件查看表内容
select id,user,pass from user;

修改表数据
uodate user set user='administrator' where id=1; 

增加一条数据
insert into user (id,user,pass,age) value (6,'yes','no',10);

增加多条数据
insert into user (id,user,pass,age) value (8,'yes2','no2',10),(7,'yes1','no1',11);

删除数据
delete from user where id=7;

删除表
delete from user;

php操作mysql语句

//打开数据库,并返回一个资源
$conn = mysql_connect('localhost', 'root' , 'root');
mysql_query('use msg' , $conn);//选择数据库
mysql_query('set names utf8' , $conn);//设置字符集

//查看数据库数据
$sql = 'select * from user';
//插入一条语句
$sql = "insert into user (id , user , pass , age) value (1 , 'admin' , 'admin' , 20)";
//修改数据
$sql = 'update user set user = "shu" where id =1';
//删除数据
$sql = "delete from user where id = 1";

$res = mysql_query($sql);//执行sql语句,返回一个资源
//循环取出所有数据
//mysql_fetch_assoc此函数同mysql_fetch_row类似
$date = array();
//从res资源中取出数据放到row中,循环row,直到等于false停止循环
while ( ($row = mysql_fetch_row($res)) != false){
    $date[] = $row;
}
print_r($date);

//mysql_errno(0);//查看报错信息
mysql_close($conn);//关闭资源
上一篇下一篇

猜你喜欢

热点阅读