mysql語句
查看當(dāng)前所有數(shù)據(jù)庫
show databases;
查看當(dāng)前所有表
show tables;
查看表結(jié)構(gòu)
desc user;
創(chuàng)建數(shù)據(jù)庫
create database msg;
創(chuàng)建數(shù)據(jù)庫
create table use (
id int not null primary key auto_increment,
user varchar(20) not null,
pass varchar(100) not null
)charset utf8;
查看表所有內(nèi)容
select * from user;
根據(jù)條件查看表內(nèi)容
select id,user,pass from user;
修改表數(shù)據(jù)
uodate user set user='administrator' where id=1;
增加一條數(shù)據(jù)
insert into user (id,user,pass,age) value (6,'yes','no',10);
增加多條數(shù)據(jù)
insert into user (id,user,pass,age) value (8,'yes2','no2',10),(7,'yes1','no1',11);
刪除數(shù)據(jù)
delete from user where id=7;
刪除表
delete from user;
php操作mysql語句
//打開數(shù)據(jù)庫,并返回一個(gè)資源
$conn = mysql_connect('localhost', 'root' , 'root');
mysql_query('use msg' , $conn);//選擇數(shù)據(jù)庫
mysql_query('set names utf8' , $conn);//設(shè)置字符集
//查看數(shù)據(jù)庫數(shù)據(jù)
$sql = 'select * from user';
//插入一條語句
$sql = "insert into user (id , user , pass , age) value (1 , 'admin' , 'admin' , 20)";
//修改數(shù)據(jù)
$sql = 'update user set user = "shu" where id =1';
//刪除數(shù)據(jù)
$sql = "delete from user where id = 1";
$res = mysql_query($sql);//執(zhí)行sql語句,返回一個(gè)資源
//循環(huán)取出所有數(shù)據(jù)
//mysql_fetch_assoc此函數(shù)同mysql_fetch_row類似
$date = array();
//從res資源中取出數(shù)據(jù)放到row中枷莉,循環(huán)row娇昙,直到等于false停止循環(huán)
while ( ($row = mysql_fetch_row($res)) != false){
$date[] = $row;
}
print_r($date);
//mysql_errno(0);//查看報(bào)錯(cuò)信息
mysql_close($conn);//關(guān)閉資源