你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了坪山免費(fèi)建站歡迎大家使用!
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("打開(kāi)文件錯(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!";
}
?
你的
echo "scriptalert('$sql');/script"; //打印測(cè)試 , 意思應(yīng)該是在客戶(hù)端通過(guò)js把$sql變量打印出來(lái)。如果是php,我想應(yīng)該是這么寫(xiě):
echo "scriptalert('{$sql}');/script"; 因?yàn)槭窃谧址幸米兞浚隳欠N寫(xiě)法可能不會(huì)有內(nèi)容。 還有 $sql變量通過(guò)上面的兩個(gè)操作,F(xiàn)n_select函數(shù)是沒(méi)有返回值的,所有$sql=$db-Fn_select獲得的$sql值是空的,是沒(méi)有內(nèi)容的。你可以在Fn_select函數(shù)中設(shè)置一個(gè)返回值,比如return true,或者return false, $sql就有值了。
PHP向MySQL數(shù)據(jù)庫(kù)中寫(xiě)入數(shù)據(jù)有三個(gè)步驟:
1,PHP和MySQL建立連接關(guān)系
2,打開(kāi)MySQL數(shù)據(jù)庫(kù)
3,接受頁(yè)面數(shù)據(jù),PHP錄入到指定的表中
1、2兩步可直接使用一個(gè)數(shù)據(jù)庫(kù)鏈接文件即可:conn.php
代碼如下
?php
mysql_connect("localhost","root","");//連接MySQL
mysql_select_db("hello");//選擇數(shù)據(jù)庫(kù)
?
當(dāng)然,前提是已經(jīng)安裝WEB服務(wù)器、PHP和MySQL,并且建立MySQL表“cnbruce”
mysql_connect()中三個(gè)參數(shù)分別為MySQL地址、MySQL用戶(hù)名和MySQL密碼
然后就是通過(guò)WEB頁(yè)面?zhèn)鬟f數(shù)據(jù),讓PHP通過(guò)SQL語(yǔ)句將數(shù)據(jù)寫(xiě)入MySQL數(shù)據(jù)庫(kù)指定的表中,比如新建文件 post.php
代碼如下
?php
require_once("conn.php");//引用數(shù)據(jù)庫(kù)鏈接文件
$uname = $_GET['n'];//GET方法為URL參數(shù)傳遞
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL語(yǔ)句插入數(shù)據(jù)
mysql_close();//關(guān)閉MySQL連接
echo "成功錄入數(shù)據(jù)";
?
測(cè)試頁(yè)面: ;p=i0514
即可向MySQL數(shù)據(jù)庫(kù)hello的members表中插入新的數(shù)據(jù)“cnbruce”到username字段、“i0514”到password字段
補(bǔ)充:讀取表
讀取表中的內(nèi)容,這里我們用while,可以根據(jù)具體情況,用for 或其他的.
代碼如下
while($row = mysql_fetch_array($result))
{
echo "div style="height:24px; line-height:24px; font-weight:bold;""; //排版代碼
echo $row['Topic'] . "br/";
echo "/div"; //排版代碼
進(jìn)入php源程序目錄中的ext目錄中,這里存放著各個(gè)擴(kuò)展模塊的源代碼,選擇你需要的模塊,比如curl模塊:cd curl執(zhí)行phpize生成編譯文件!
phpize在PHP安裝目錄的bin目錄/usr/local/php5/bin/phpize運(yùn)行時(shí),
可能會(huì)報(bào)錯(cuò):Cannot find autoconf. Please check your autoconf installation andthe $PHP_AUTOCONFenvironment variable is set correctly and then rerun thisscript.,需要安裝autoconf:yum install autoconf(RedHat或者CentOS)、apt-get installautoconf(Ubuntu Linux)!
執(zhí)行/usr/local/php5/bin/php -v這個(gè)命令時(shí),php會(huì)去檢查配置文件是否正確,
如果有配置錯(cuò)誤,這里會(huì)報(bào)錯(cuò),可以根據(jù)錯(cuò)誤信息去排查!
網(wǎng)頁(yè)標(biāo)題:php如何編寫(xiě)數(shù)據(jù)庫(kù),php數(shù)據(jù)庫(kù)搭建
當(dāng)前URL:http://chinadenli.net/article34/hsjsse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站策劃、服務(wù)器托管、ChatGPT、網(wǎng)頁(yè)設(shè)計(jì)公司、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)