PHP MySQL 创建表
PHP mysql_query() 函数用于创建表。自 PHP 5.5 起, mysql_query() 函数被 弃用 。现在建议使用这两个替代函数中的一个。
- mysqli_query()
- PDO::__query()
PHP MySQLi 创建表示例
示例
<?php
host = 'localhost:3306';user = '';
pass = '';dbname = 'test';
conn = mysqli_connect(host, user,pass,dbname);
if(!conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';
sql = "create table emp5(id INT AUTO_INCREMENT,name VARCHAR(20) NOT NULL,
emp_salary INT NOT NULL,primary key (id))";
if(mysqli_query(conn, sql)){ echo "Table emp5 created successfully";
}else{
echo "Could not create table: ". mysqli_error(conn);
}
mysqli_close($conn);
?>
输出:
Connected successfully
Table emp5 created successfully