More than one MySQL databases are also created in PHP.MySQL Database; The ‘CREATE DATABASE database_name’ statement is used to create.
Creat Database using MySQLi
Code :
<?php $server = "localhost"; $user = "root"; $password = ""; $conn = mysqli_connect($server, $user, $password); if($conn){ echo "Connected successfully."; } else{ echo "Connection failed : ".mysqli_connect_error(); } $createdb = "CREATE DATABASE expertstutorial"; if (mysqli_query($conn, $createdb)) { echo "Database Created successfully."; } else { echo "database Creation failed : " . mysqli_error($conn); } mysqli_close($conn); ?>
Creating Database using PDO
code:
<?php $server = 'localhost'; $user = 'root'; $password = ''; try{ $conn = new PDO("mysql:host=$server;", $user, $password); echo "Connected successfully."; $createdb = "CREATE DATABASE expertstutorial"; $conn->exec($createdb); echo "Database Created successfully"; } catch(PDOException $e){ echo "Connection failed : " . $e->getMessage(); } $conn = null; ?>