php中链接Mysql实例
$conn = mysqli_connect(host,username,password,dbname,port,socket)
mysqli_close()
创建数据:
CREATE DATABASE 数据库名称
例子:CREATE DATABASE RUNDOOB
删除数据库:drop database<数据库名>
删除名字为RUNDOOB的数据库:drop database RUNDOOB
利用mysql mysqladmin命令在终端删除数据库:mysqldamin -u root -p drop RUNDOOB
ENTER PASSWORD:
利用·mysqli_query(connect,query,resultmode)·
connect(连接方式必须的)query(要执行的SQL语句)
$sql ="drop database RUNDOOB"
在PHP中利用mysqli_query($conn,$sql);
服务器下链接数据库:
mysql -u root -p
Enter password:**********
mysql>use RUNDOOB
Database chaged
mysql> //此后命令全部为在mysql(RUNDOOB)下执行。
在PHP中使用脚本连接和数据库 mysql_select_db(connnect,daname) connect是连接方式是必须的,dbname是数据的名称。
MYSQL支持标准的SQL类型
创建数据表:
CRATE TABLE IF NOT EXISTS `rundoob_tb`(
`id` INT UNSINGNED AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`author` varchar(10) NOT NULL
`submission_date` DATE,
PRIMARY KEY(`runoob_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf-8;
CREATE TABLE runoob_tbl(
runoob_id INT NOT NULL AUTO_INCREMENT,
runoob_title VARCHAR(100) NOT NULL,
runoob_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( runoob_id )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
在PHP中创建一个新表:
$sql ="
/*此处写要执行的sql语句*/
"
mysqli_select_db($conn,'runnoob');
mysqli_query($conn,$sql);
mysql_close();
mysql中数据的插入:
语法:
INSERT INTO table_name(field1,field2,field3)
VALUES(value1,value2,value3);
例子:
INSERT INTO runoob_tbl
(runoob_title,runoob_author,submission_date)
VALUES
("学习","例子",NOW());
在PHP使用sql插入数据:
$runoob_id ='学习 ';
$runoob_author ='Runoob';
$runoob_data = now();
$sql = "INSERT INTO runoob_tbl ".
"(runoob_title,runoob_author, submission_date) ".
"VALUES ".
"('$runoob_title','$runoob_author','$submission_date')";
PHP中选择数据库
mysqli_select_db($conn,dbname);
mysqli_query($conn,$sql);
WHERE语法
SELECT field1, field2,...fieldN FROM table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
使用BINARY来区分大小写
更新数据UPDATE
UPDATE 表名 SET 列1 =新的值 ,列2=新的值
例子:UPDATE runoob_tbl SET runoob_id='学习' where runoob_id=3;
删除:
DELETE FROM table_name[WHRER Clause]
SELECT field1, field2,...fieldN
FROM table_name
LIKE子句:
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
Mysql UNION操作符:联合查询
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
GROUP BY对一个列或者多个列进行分组;
例子:从一个表中查询每个人信息的条数。
SELECET name,COUNT(*)FROM employee GROUP BY name;
使用WITH ROLLUP
SELECT name,sum(sigin) as sigin_count FROM employee GROUP BY name WITH ROLLUP;
网友评论