Ngôn ngữ PHP - MySQLi Tạo DB
Dùng MySQLi tạo cơ sở dữ liệu
Để tạo cơ sở dữ liệu ta dùng ta có thể chọn 1 trong 2 cách sau:
- mysqli_query()
- PDO::__query()
Ví dụ:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hiepsiit";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
die('Kết nối thất bại:'.mysqli_connect_error());
}else{
echo"kết nối thành công";
}
$sql = 'CREATE Database demo';
if(mysqli_query( $conn,$sql)){
echo "Đã tạo thành công cơ sở dữ liệu.";
}else{
echo "Không tạo được cơ sở dữ liệu ".mysqli_error($conn);
}
mysqli_close($conn);
?>
Xuất ra kết quả:
kết nối thành công
Đã tạo thành công cơ sở dữ liệu.