數(shù)據(jù)庫(kù)是相互關(guān)聯(lián)的數(shù)據(jù)的集合,我們可以從數(shù)據(jù)庫(kù)中有效地檢索,插入和刪除數(shù)據(jù),并以表格,視圖,模式等形式組織數(shù)據(jù)。今天將要介紹如何通過(guò)PHP來(lái)創(chuàng)建MySQL數(shù)據(jù)庫(kù)
PHP創(chuàng)建MySQL數(shù)據(jù)庫(kù)的基本步驟:
(1)建立PHP腳本與MySQL服務(wù)器的連接
(2)如果連接成功,編寫SQL查詢以創(chuàng)建數(shù)據(jù)庫(kù)并將其存儲(chǔ)在字符串變量中
(3)執(zhí)行查詢
(4)關(guān)閉數(shù)據(jù)庫(kù)
接下來(lái)在文章中將為大家具體介紹PHP創(chuàng)建數(shù)據(jù)庫(kù)的過(guò)程
?php
header("Content-Type: text/html; charset=utf8");
$servername = "localhost";
$username = "username"
$password = "password"
//創(chuàng)建鏈接
$conn = new mysqli($servername,$username,$password);
if($conn->connect_error){
die("連接失敗" .$conn->connect_error);
}
//創(chuàng)建一個(gè)為newDemo的數(shù)據(jù)庫(kù)
$sql = "CREATE DATABASE newDemo";
//mysqli_query() 函數(shù)用于執(zhí)行某個(gè)針對(duì)數(shù)據(jù)庫(kù)的查詢。
if($conn->query($sql) === TRUE){
echo "數(shù)據(jù)庫(kù)創(chuàng)建成功";
}
else {
echo "Error creating database: " . $conn->error;
}
//關(guān)閉數(shù)據(jù)庫(kù)
$conn->close();
?>
效果圖:


您可能感興趣的文章:- 創(chuàng)建數(shù)據(jù)庫(kù)php代碼 用PHP寫出自己的BLOG系統(tǒng)
- php桌面中心(一) 創(chuàng)建數(shù)據(jù)庫(kù)