用php寫一數(shù)據(jù)庫 php數(shù)據(jù)庫設(shè)計

php寫入數(shù)據(jù)庫

PHP向MySQL數(shù)據(jù)庫中寫入數(shù)據(jù)有三個步驟:

專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)隆堯免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

1,PHP和MySQL建立連接關(guān)系

2,打開MySQL數(shù)據(jù)庫

3,接受頁面數(shù)據(jù),PHP錄入到指定的表中

1、2兩步可直接使用一個數(shù)據(jù)庫鏈接文件即可:conn.php

代碼如下

?php

mysql_connect("localhost","root","");//連接MySQL

mysql_select_db("hello");//選擇數(shù)據(jù)庫

?

當然,前提是已經(jīng)安裝WEB服務器、PHP和MySQL,并且建立MySQL表“cnbruce”

mysql_connect()中三個參數(shù)分別為MySQL地址、MySQL用戶名和MySQL密碼

然后就是通過WEB頁面?zhèn)鬟f數(shù)據(jù),讓PHP通過SQL語句將數(shù)據(jù)寫入MySQL數(shù)據(jù)庫指定的表中,比如新建文件 post.php

代碼如下

?php

require_once("conn.php");//引用數(shù)據(jù)庫鏈接文件

$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語句插入數(shù)據(jù)

mysql_close();//關(guān)閉MySQL連接

echo "成功錄入數(shù)據(jù)";

?

測試頁面: ;p=i0514

即可向MySQL數(shù)據(jù)庫hello的members表中插入新的數(shù)據(jù)“cnbruce”到username字段、“i0514”到password字段

補充:讀取表

讀取表中的內(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"; //排版代碼

如何實現(xiàn)PHP自動創(chuàng)建數(shù)據(jù)庫

你做好程序以后,把數(shù)據(jù)庫導出成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());

}

//導入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!";

}

?

php?寫入數(shù)據(jù)庫?例子

?php

//?以?MySQL?為例:

mysql_connect('127.0.0.1',?'root',?'root',?3306);??//?連接數(shù)據(jù)庫

mysql_select_db('test');???????????????????????????//?選擇數(shù)據(jù)庫

mysql_query('set?names?utf8');?????????????????????//?執(zhí)行SQL

//?插入數(shù)據(jù)語句

$sql?=?"INSERT?INTO?table?(username,?password)?VALUES?('Jack@163.com',?'123456')";

$r?=?mysql_query($sql);

if?(mysql_affected_rows())?{

echo?'新增成功';

}?else?{

echo?mysql_error();

}

如何用php創(chuàng)建mysql數(shù)據(jù)庫

使用EclipsePHP Studio 3 創(chuàng)建一個PHP工程名稱為test1,在工程名下面userinfo的文件夾,然后在文件夾創(chuàng)建一個PHP文件(userinfo_create.php):

2

打開我們創(chuàng)建PHP文件:

先設(shè)置 地址,賬號,密碼:

$url = "127.0.0.1";//連接數(shù)據(jù)庫的地址

$user = "root"; //賬號

$password = "root";//密碼

//獲取連接$con = mysql_connect($url,$user,$password);

if(!$con){

die("連接失敗".mysql_error());

}

3

設(shè)置具體連接的數(shù)據(jù),那我們這兒連接test數(shù)據(jù)庫,我們通過Navicat 打開mysql 數(shù)據(jù)庫

mysql_select_db("test");

網(wǎng)站欄目:用php寫一數(shù)據(jù)庫 php數(shù)據(jù)庫設(shè)計
網(wǎng)址分享:http://bm7419.com/article26/ddcocjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站做網(wǎng)站、面包屑導航、自適應網(wǎng)站標簽優(yōu)化、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站