设为首页 加入收藏

TOP

PHP-MySQL基本操作(一)
2019-08-23 00:36:55 】 浏览:41
Tags:PHP-MySQL 基本操作

PHP-MySQL基本操作

  1 <?php
  2 
  3 // 1.防止页面中文乱码
  4 header("content-type:text/html;charset=utf-8");
  5 
  6 // 链接数据库
  7 @$conn = new mysqli('localhost', 'root', '');
  8 echo $conn == true ? '数据库连接成功' : '数据库连接失败';
  9 
 10 // 2.连接到MySQL后设置字符集(防止存入中文乱码)
 11 mysqli_set_charset($conn, 'utf8');
 12 
 13 // 创建数据库
 14 $createDatabaseSql = "create database ProsperLee01 charset utf8";
 15 // 删除数据库
 16 $dropDatabaseSql = "drop database ProsperLee01";
 17 // 查询数据库
 18 $showDatabaseSql = "show databases";
 19 // 查询创建的数据库信息
 20 $showCreateDatabaseSql = "show create database ProsperLee01";
 21 // 修改数据库(不能修改数据库的名字,只能修改字符集和校对集)语法:alter database 数据库 character set=新的字符集 collate=新的校对集
 22 $alterDatabaseSql1 = "alter database ProsperLee01 character set=gbk collate=gbk_Chinese_ci";
 23 $alterDatabaseSql2 = "alter database ProsperLee01 charset=utf8 collate=utf8_general_ci";
 24 // 查询字符集
 25 $showCharsetSql = "show charset";
 26 // 查询校对集
 27 $showCollationSql = "show collation";
 28 // 使用数据库
 29 $useDatabaseSql = "use ProsperLee01";
 30 // 创建数据表 语法 CREATE TABLE 库名 表名 (字段名 字段类型 字段选项, 字段名 字段类型 字段选项···) 表选项; (表选项:表字符集(charset=utf8)和数据引擎(engine=引擎))
 31 $createTableSql =<<<sss
 32     CREATE TABLE `prosperlee01`.`Lee01` (
 33         `name` varchar(20) DEFAULT '小明同学' NOT NULL COMMENT '姓名',
 34         `sex` varchar(5) NULL COMMENT '性别',
 35         `age` int(2) NULL COMMENT '年龄',
 36         `index` int NOT NULL AUTO_INCREMENT COMMENT '序号',
 37         `id` varchar(8) NOT NULL COMMENT 'id',
 38         PRIMARY KEY (`index`, `id`)
 39     ) ENGINE=InnoDB DEFAULT CHARSET=utf8
 40 sss;
 41 // 查询数据表
 42 $showTablesSql = "show tables";
 43 // 查看数据表结构
 44 $descTableSql = "desc lee01";
 45 // 查询创建的数据表
 46 $showCreateTableSql = "show create table Lee01";
 47 // 修改表名 alter table 旧表名 rename [to] 新表名
 48 $alterTableNameSql = "alter table lee01 rename to newlee01";
 49 // 添加新字段
 50 $alterTableAddFieldSql = "alter table lee01 add realname varchar(50) not null default '用户名' comment '真实姓名'";
 51 // 删除字段 alter table 表名 drop column 字段名
 52 $alterTableDropFieldSql = "alter table lee01 drop column phone";
 53 // 修改表的字段类型   ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型定义;
 54 $alterTableModifyFieldSql = "ALTER TABLE lee01 MODIFY COLUMN id VARCHAR(32)";
 55 // 修改表的字段名(修改后原始字段存的值不会变,新存入的值不填写变成默认值) alter table 表名 change 原字段名  新字段名  字段的类型
 56 $alterTableChangeFieldSql = "ALTER TABLE lee01 change realname phone VARCHAR(11) default '01234567890' comment '电话号'";
 57 // 插入数据 INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
 58 $insertIntoDataSql1 = "INSERT INTO Lee01 (`sex`,`age`,`id`) VALUES ('女',20,'88888888')";
 59 $insertIntoDataSql2 = "INSERT INTO Lee01 (`name`,`sex`,`age`,`id`) VALUES ('Lee','女',20,'66666666')";
 60 $insertIntoDataSql = <<<sss
 61     INSERT INTO Lee01 ( `name`, `sex`, `age`, `id` )
 62     VALUES
 63         ( '张三', '女', 16, '12345678' ),
 64         ( '李四', '男', 18, '87654321' ),
 65         ( '王五', '女', 20, '22222222' ),
 66         ( '赵六', '女', 21, '11111111' ),
 67         ( '八爷', '男', 26, '14725836' ),
 68         ( '九少', '男', 16, '66666666' )
 69 sss;
 70 // 查询语句
 71 $selectDataSql1 = "select * from lee01";
 72 $selectDataSql2 = "select * from lee01 where sex = '女' and age = '20'";
 73 $selec
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇服务器部署Apache+PHP+MYSQL+Lara.. 下一篇[PHP] curl访问https与CA证书问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目