創(chuàng)建數(shù)據(jù)庫:create database 數(shù)據(jù)庫名
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),漢陰企業(yè)網(wǎng)站建設(shè),漢陰品牌網(wǎng)站建設(shè),網(wǎng)站定制,漢陰網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,漢陰網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
創(chuàng)建數(shù)據(jù)表:
CREATE TABLE `users` (
`id` tinyint(10) auto_increment primary key NOT NULL,
`username` varchar(30) NOT NULL,
`age` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
執(zhí)行這兩個(gè)sql語句就行
下個(gè)開源免費(fèi)的數(shù)據(jù)庫mysql安裝程序 和客戶端;
然后再使用SQL建庫,或者就直接使用test數(shù)據(jù)庫
接著創(chuàng)建表、建表結(jié)構(gòu)
CREATE TABLE 的語法是:
CREATE TABLE "表格名"
("欄位 1" "欄位 1 資料種類",
"欄位 2" "欄位 2 資料種類",
... )
若我們要建立顧客表格,我們就打入以下的 SQL:
CREATE TABLE customer
(First_Name char(50),
Last_Name char(50),
Address char(50),
City char(50),
Country char(25),
Birth_Date date)
其他的SQL可以參考
先mysql_connect()到mysql再做mysql_query()的相關(guān)操作。
====================================================================
?php
$server = "localhost";
$username = "root";
$password = "";
$database = "myabc";
$ranks = array(
1="newbie",
2="new member",
3="member",
4="high member",
5="very high member",
6="supreme member",
7="ultra member",
8="godlike member",
9="god member",
10="low god",
11="medium god",
12="high god",
13="very high god",
14="supreme god",
15="ultra god",
16="perfect"
);
$couldNotOpenDatabase = "Could not open databaseBR\n please check your settings in config.php";
$couldNotConnectMysql="Could not connect Mysql!";
$conn=mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
if (mysql_select_db($database,$conn))
{//數(shù)據(jù)庫存在,做相應(yīng)操作
}
else
{//數(shù)據(jù)庫不存在,創(chuàng)建一個(gè),并做相應(yīng)操作
$query = "CREATE DATABASE $database";
$result = mysql_query($query);
mysql_select_db($database,$conn)or die ($couldNotOpenDatabase);
}
?
你做好程序以后,把數(shù)據(jù)庫導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫
2、讀取這個(gè)sql文件里的sql語句,并執(zhí)行
3、生成一個(gè)數(shù)據(jù)庫連接參數(shù)的php文件
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
if?(mysql_query("CREATE?DATABASE?my_db",$con))
{
echo?"Database?created";
}
else
{
echo?"Error?creating?database:?"?.?mysql_error();
}
mysql_close($con);
?
?php
class?ReadSql?{
//數(shù)據(jù)庫連接
protected?$connect?=?null;
//數(shù)據(jù)庫對象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語句集
public?$sqlArr?=?array();
public?function?__construct($host,?$user,?$pw,?$db_name)?{
$host?=?empty($host)???C("DB_HOST")?:?$host;
$user?=?empty($user)???C("DB_USER")?:?$user;
$pw?=?empty($pw)???C("DB_PWD")?:?$pw;
$db_name?=?empty($db_name)???C("DB_NAME")?:?$db_name;
//連接數(shù)據(jù)庫
$this-connect?=?mysql_connect($host,?$user,?$pw)?or?die("Could?not?connect:?"?.?mysql_error());
$this-db?=?mysql_select_db($db_name,?$this-connect)?or?die("Yon?can?not?select?the?table:"?.?mysql_error());
}
//導(dǎo)入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打開文件錯(cuò)誤");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
}
}
}
//獲取sql語句數(shù)組
public?function?GetSqlArr()?{
//去除注釋
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?創(chuàng)建數(shù)組
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
}?else?{
$this-sqlArr[]?=?$v;
}
}
}
//執(zhí)行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql語句錯(cuò)誤:第"?.?$k?.?"行"?.?mysql_error());
}
}
return?true;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql?=?new?ReadSql("localhost",?"root",?"",?"log_db");
$rst?=?$sql-Import("./log_db.sql");
if?($rst)?{
echo?"Success!";
}
?