自从PHP4.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 successfuly.