新聞中心
如何用php創(chuàng)建mysql數(shù)據(jù)庫(kù)
使用EclipsePHP Studio 3 創(chuàng)建一個(gè)PHP工程名稱為test1,在工程名下面userinfo的文件夾,然后在文件夾創(chuàng)建一個(gè)PHP文件(userinfo_create.php):
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的共和網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
2
打開我們創(chuàng)建PHP文件:
先設(shè)置 地址,賬號(hào),密碼:
$url = "127.0.0.1";//連接數(shù)據(jù)庫(kù)的地址
$user = "root"; //賬號(hào)
$password = "root";//密碼
//獲取連接$con = mysql_connect($url,$user,$password);
if(!$con){
die("連接失敗".mysql_error());
}
3
設(shè)置具體連接的數(shù)據(jù),那我們這兒連接test數(shù)據(jù)庫(kù),我們通過Navicat 打開mysql 數(shù)據(jù)庫(kù)
mysql_select_db("test");
PHP創(chuàng)建數(shù)據(jù)庫(kù)
試試這樣:
$chuang = "CREATE DATABASE `ok` DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci";
phpstudy 怎么建數(shù)據(jù)庫(kù)
phpstudy數(shù)據(jù)庫(kù)創(chuàng)建步驟:
1、點(diǎn)擊打開phpstudy軟件,然后點(diǎn)擊mySQL管理器;
2、進(jìn)入到PHPmyadmin登陸界面,默認(rèn)賬號(hào)和密碼都是root;
3、登陸進(jìn)去后,如圖所示樣式;
4、然后,點(diǎn)擊數(shù)據(jù)庫(kù),輸入想要的數(shù)據(jù)名稱,如:new,這個(gè)隨便??;
5、點(diǎn)擊創(chuàng)建后,成功后,
如何在php創(chuàng)建數(shù)據(jù)庫(kù)與數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)庫(kù):create database 數(shù)據(jù)庫(kù)名
創(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語(yǔ)句就行
如何實(shí)現(xiàn)PHP自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)
你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫(kù)
2、讀取這個(gè)sql文件里的sql語(yǔ)句,并執(zhí)行
3、生成一個(gè)數(shù)據(jù)庫(kù)連接參數(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ù)庫(kù)連接
protected?$connect?=?null;
//數(shù)據(jù)庫(kù)對(duì)象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語(yǔ)句集
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ù)庫(kù)
$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語(yǔ)句數(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語(yǔ)句錯(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!";
}
?
網(wǎng)站名稱:php新建數(shù)據(jù)庫(kù) php數(shù)據(jù)庫(kù)添加數(shù)據(jù)
URL標(biāo)題:http://www.dlmjj.cn/article/dococsg.html