- 相關(guān)推薦
PHP如何創(chuàng)建數(shù)據(jù)庫(kù)
MySQL是最流行的開(kāi)源數(shù)據(jù)庫(kù)服務(wù)器,那我要使用PHP在MySQL的數(shù)據(jù)庫(kù)創(chuàng)建一張表,實(shí)現(xiàn)通過(guò)PHP操作數(shù)據(jù),那我們先要獲取連接,拿到MySQL數(shù)據(jù)庫(kù)的賬號(hào)密碼。下面就跟隨小編一起來(lái)看看吧!
PHP創(chuàng)建數(shù)據(jù)庫(kù)
$con = mysql_connect('localhost', 'root', 'root');
/************************在數(shù)據(jù)庫(kù)中創(chuàng)建表*************************/
if (!$con) {
die ('連接數(shù)據(jù)庫(kù)出錯(cuò): ' . mysql_error());
}
$database="my_db_name";
$sqlDatabase = 'create database '.$database;
if(mysql_query($sqlDatabase, $con))
{
echo "恭喜你,數(shù)據(jù)庫(kù)".$database."創(chuàng)建成功了!";
}
else
{
echo "創(chuàng)建數(shù)據(jù)庫(kù)出錯(cuò),錯(cuò)誤號(hào):".mysql_errno()." 錯(cuò)誤原因:".mysql_error();
}
/******************************end***************************************/
/************************在數(shù)據(jù)庫(kù)中創(chuàng)建表*************************/
mysql_select_db("my_db_name",$con);
$sqlTable="create table my_table_name (
id int unsigned not null auto_increment primary key,
a var20),
b var2),
c var1))";
if(mysql_query($sqlTable))
{
echo "恭喜你,數(shù)據(jù)表創(chuàng)建成功了!";
}
else
{
echo "創(chuàng)建數(shù)據(jù)表出錯(cuò),錯(cuò)誤號(hào):".mysql_errno()." 錯(cuò)誤原因:".mysql_error();
}
/******************************end***************************************/
/*************************在表中寫(xiě)入數(shù)據(jù)*********************************/
//保證已經(jīng)執(zhí)行:
//mysql_connect("localhost","username","passcode");//連接MySQL
//mysql_select_db("db_name");//選擇數(shù)據(jù)庫(kù)
$sql = " into my_table_name(a,b) values ('123','45')"; //'--'單引號(hào)內(nèi)可以是變量
if(mysql_query($sql))//借SQL語(yǔ)句插入數(shù)據(jù)
{
echo "恭喜你,表中寫(xiě)入數(shù)據(jù)成功了!";
}
/******************************end***************************************/
mysql_close();
?>
【PHP如何創(chuàng)建數(shù)據(jù)庫(kù)】相關(guān)文章:
如何在PHP中連接MySQL數(shù)據(jù)庫(kù)04-13
php網(wǎng)站如何連接到遠(yuǎn)程mysql數(shù)據(jù)庫(kù)04-11