自从PHP 4.3起,mysql_create_db()
函数已被弃用。 现在,建议使用以下2
种替代方法之一。
- mysqli_query()
- PDO::__query()
PHP MySQLi创建数据库
示例
<?php
$host = 'localhost:3306';
$user = 'root'; // MySQL用户账号
$pass = '';
$conn = mysqli_connect($host, $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully<br/>';
$sql = 'CREATE Database mydb';
if(mysqli_query( $conn,$sql)){
echo "Database mydb created successfully.";
}else{
echo "Sorry, database creation failed ".mysqli_error($conn);
}
mysqli_close($conn);
?>
执行上面代码,输出结果如下 -
Connected successfully
Database mydb created successfully.