PHP MySQL創建數據庫

自從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.