phpstudy數(shù)據(jù)庫創(chuàng)建步驟:
成都創(chuàng)新互聯(lián)公司成立與2013年,先為阿拉爾等服務(wù)建站,阿拉爾等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為阿拉爾企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1、點(diǎn)擊打開phpstudy軟件,然后點(diǎn)擊mySQL管理器;
2、進(jìn)入到PHPmyadmin登陸界面,默認(rèn)賬號和密碼都是root;
3、登陸進(jìn)去后,如圖所示樣式;
4、然后,點(diǎn)擊數(shù)據(jù)庫,輸入想要的數(shù)據(jù)名稱,如:new,這個隨便?。?/p>
5、點(diǎn)擊創(chuàng)建后,成功后,如圖所示;
你做好程序以后,把數(shù)據(jù)庫導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫
2、讀取這個sql文件里的sql語句,并執(zhí)行
3、生成一個數(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("打開文件錯誤");
}?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語句錯誤:第"?.?$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!";
}
?
創(chuàng)建數(shù)據(jù)庫:create database 數(shù)據(jù)庫名
創(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í)行這兩個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)建一個,并做相應(yīng)操作
$query = "CREATE DATABASE $database";
$result = mysql_query($query);
mysql_select_db($database,$conn)or die ($couldNotOpenDatabase);
}
?