1.创建数据库
(1) 命令行创建
[root@host]# mysqladmin -u root -p create RUNOOB Enter password:******
(2) php创建
语法:
mysqli_query(connection,query,resultmode);
实例:
<?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接错误: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = 'CREATE DATABASE RUNOOB'; $retval = mysqli_query($conn,$sql ); if(! $retval ) { die('创建数据库失败: ' . mysqli_error($conn)); } echo "数据库 RUNOOB 创建成功 "; mysqli_close($conn); ?>
2.创建数据表
(1) 命令行创建
语法:
CREATE TABLE table_name (column_name column_type);
实例:
CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED 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;
(2) php 创建
语法:
mysqli_query(connection,query,resultmode);
实例:
<?php $dbhost = 'localhost:3306'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = "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; "; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('数据表创建失败: ' . mysqli_error($conn)); } echo "数据表创建成功 "; mysqli_close($conn); ?>
.