php連接數(shù)據(jù)庫及調(diào)用 php怎么和mysql數(shù)據(jù)庫連接

thinkphp怎么連接數(shù)據(jù)庫

thinkphp連接數(shù)據(jù)庫的方法:

創(chuàng)新互聯(lián)建站10多年企業(yè)網(wǎng)站制作服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站制作及推廣,對混凝土攪拌站等多個行業(yè)擁有多年的網(wǎng)站維護(hù)經(jīng)驗的網(wǎng)站建設(shè)公司。

ThinkPHP內(nèi)置了抽象數(shù)據(jù)庫訪問層,把不同的數(shù)據(jù)庫操作封裝起來,只需要使用公共的Db類進(jìn)行操作,而無需針對不同的數(shù)據(jù)庫寫不同的代碼和底層實現(xiàn),Db類會自動調(diào)用相應(yīng)的數(shù)據(jù)庫驅(qū)動來處理。目前的數(shù)據(jù)庫包括Mysql、SqlServer、PgSQL、Sqlite、Oracle、Ibase、Mongo,也包括對PDO的支持,如果應(yīng)用需要使用數(shù)據(jù)庫,必須配置數(shù)據(jù)庫連接信息,數(shù)據(jù)庫的配置文件有多種定義方式。

常用的配置方式是在項目配置文件中添加下面的參數(shù):

?php

//項目配置文件

return array(

//數(shù)據(jù)庫配置信息

'DB_TYPE' = 'mysql', // 數(shù)據(jù)庫類型

'DB_HOST' = 'localhost', // 服務(wù)器地址

'DB_NAME' = 'thinkphp', // 數(shù)據(jù)庫名

'DB_USER' = 'root', // 用戶名

'DB_PWD' = '', // 密碼

'DB_PORT' = 3306, // 端口

'DB_PREFIX' = 'think_', // 數(shù)據(jù)庫表前綴

//其他項目配置參數(shù)

// ...

);

需要注意的是,ThinkPHP的數(shù)據(jù)庫連接的惰性的,所以并不是在實例化的時候就連接數(shù)據(jù)庫,而是在有實際的數(shù)據(jù)操作的時候才會去連接數(shù)據(jù)庫(額外的情況是,在系統(tǒng)第一次實例化模型的時候,會自動連接數(shù)據(jù)庫獲取相關(guān)模型類對應(yīng)的數(shù)據(jù)表的字段信息)。

php調(diào)用數(shù)據(jù)庫字段

我說一下幾個步驟:

1、首先你得有一個存儲這些數(shù)據(jù)的數(shù)據(jù)庫表,比如數(shù)據(jù)庫表的結(jié)構(gòu)是這樣的。

數(shù)據(jù)庫表名為:user

字段:編號(id),姓名(name),手機(jī)(mobile),產(chǎn)品名稱(productName) 主鍵為id

2、實現(xiàn)你需要的功能:

第一步:你需要連接數(shù)據(jù)庫,有一個連接數(shù)據(jù)庫的文件:conn.php。內(nèi)容如下:

// 我假設(shè)你的數(shù)據(jù)庫是mysql的,假設(shè)你的數(shù)據(jù)庫用戶名為root,密碼為123456,根據(jù)你數(shù)據(jù)庫的實際情況改寫成你的。數(shù)據(jù)庫名稱假設(shè)為db_889888658

?php

$conn=mysql_connect("localhost","root","123456") or die("數(shù)據(jù)庫連接失敗,請檢查用戶名或密碼");

mysql_select_db("db_889888658",$conn);

mysql_query("SET NAMES 'gb2312'");

?

第二步:你需要一個添加數(shù)據(jù)的表單,就相當(dāng)于一個注冊或添加數(shù)據(jù)的頁面。如文件為:add.html內(nèi)容如下:

form action="reg.php" method="post"

input type="text" name="name"br/

input type="text" name="mobile"br/

input type="text" name="productName"/br

input type="submit" name="submit" value="添加數(shù)據(jù)"

/form

第三步:寫一個處理你表單提交的數(shù)據(jù)的文件reg.php。內(nèi)容如下:

?php

include "conn.php";

if(isset($_POST["submit"])){

$name=$_POST["name"];

$mobile=$_POST["mobile"];

$productName=$_POST["productName"];

$sql="INSERT INTO 'user'(id,name,mobile,productName) VALUES (NULL,$name,$mobile,$productName)";

$query=mysql_query($sql);

$num=mysql_affected_rows($conn);

if($num=1){

echo "scriptalert('數(shù)據(jù)添加成功');location.href='add.html';/script";

}else{

echo "scriptalert('數(shù)據(jù)添加失敗');history.back();/script";

}

}

?

第四步,第三步已經(jīng)實現(xiàn)你說的第一個功能。下面說一下你的第二個功能。寫一個表單,輸入你要查詢的手機(jī)號,點擊“查詢”按鈕查詢你想要的字段。

?php

if($_POST["submit"]){

$mobile=$_POST["mobile"];

if(!empty($mobile)){

include "conn.php";

$sql="SELECT * FROM 'user' WHERE 'mobile'='$mobile'";

$query=mysql_query($sql);

while($rs=mysql_fetch_array($query)){

$str="查詢結(jié)果:br/";

$str.="用戶名:".$rs["name"]."?";

$str.="產(chǎn)品名:".$rs["name"]."?";

}

echo "您查詢的手機(jī)號為".$mobile."的數(shù)據(jù)信息如下:br/";

echo $str;

}else{

echo "請輸入手機(jī)號";

}

}

?

form action="" method="post"

請輸入您要查詢的手機(jī)號:input type="text" name="mobile" input type="submit" name="submit" value="查詢"

/form

phpstorm如何調(diào)用數(shù)據(jù)庫

打開phpstorm,打開Database窗口,如下圖:

配置mysql連接,如下圖:

填寫mysql地址,用戶名,密碼,如果沒有安裝驅(qū)動,要先安裝驅(qū)動

測試數(shù)據(jù)庫能否連接成功:

保存配置,保存時,會提示設(shè)置密碼:

讀取數(shù)據(jù)庫表,及根據(jù)條件查詢修改:

PHP調(diào)用三種數(shù)據(jù)庫的方法(3)

Oracle(甲骨文)是世界上最為流行的關(guān)系數(shù)據(jù)庫。它是大公司推崇的工業(yè)化的強(qiáng)有力的引擎。我們先看看其相關(guān)的函數(shù):

(1)integer

ora_logon(string

user

,

string

password)

開始對一個Oracle數(shù)據(jù)庫服務(wù)器的連接。

(2)integer

ora_open(integer

connection)

打開給出的連接的游標(biāo)。

(3)integer

ora_do(integer

connection,

string

query)

在給出的連接上執(zhí)行查詢。PHP生成一個指示器,解析查詢,并執(zhí)行之。

(4)integer

ora_parse(integer

cursor,

string

query)

解析一個查詢并準(zhǔn)備好執(zhí)行。

(5)boolean

ora_exec(integer

cursor)

執(zhí)行一個先前由ora_parse函數(shù)解析過的查詢。

(6)boolean

ora_fetch(integer

cursor)

此函數(shù)會使得一個執(zhí)行過的查詢中的行被取到指示器中。這使得您可以調(diào)用ora_getcolumn函數(shù)。

(7)string

ora_getcolumn(integer

cursor,

integer

column)

返回當(dāng)前的值。列由零開始的數(shù)字索引。

(8)boolean

ora_logoff(integer

connection)

斷開對數(shù)據(jù)庫服務(wù)器的鏈接。

以下是向ORACLE數(shù)據(jù)庫插入數(shù)據(jù)的示例程序:

html

headtitle向ORACLE數(shù)據(jù)庫中插入數(shù)據(jù)/title/head

body

form

action="?echo

$PHP_SELF;?"

method="post"

table

border="1"

cellspacing="0"

cellpadding="0"

tr

thID/th

thname/th

thDescription/th

/tr

tr

tdinput

type="text"

name="name"

maxlength="50"

size="10"/td

tdinput

type="text"

name="email"

maxlength="255"

size="30"/td

tdinput

type="text"

name="Description"

maxlength="255"

size="50"/td

/tr

tr

align="center"

td

colspan="3"input

type="submit"

value="提交" input

type="reset"

value="重寫"/td

/tr

/table

/form

?

//先設(shè)置兩個環(huán)境變量ORACLE_HOME,ORACLE_SID

putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");

putenv("ORACLE_SID=ora8");

//設(shè)置網(wǎng)頁顯示中文

putenv("NLS_LANG=Simplified_Chinese.zhs16cgb231280");

if($connection=ora_logon("scott","tiger"))

{

//庫表test有ID,name,Description三項

$sql

=

'insert

into

test(ID,name,Description)

values

';

$sql

.=

'(''

.

$ID

.

'',''

.

$name

.

'',''.

$Description

.

'')';

if($cursor=ora_do($connect,$sql))

{

print("insert

finished!");

}

$query

=

'select

*

from

test';

if($cursor=ora_do($connect,$query))

{

ora_fetch($cursor);

$content0=ora_getcolumn($cursor,0);

$content1=ora_getcolumn($cursor,1);

$content2=ora_getcolumn($cursor,2);

print("$content0");

print("$content1");

print("$content2");

ora_close($cursor);

}

ora_logoff($connection);

}

?

/body

/html

怎么將php與數(shù)據(jù)庫連接

php鏈接mysql必備條件:

已安裝mysql數(shù)據(jù)庫;

檢查php環(huán)境是否已開啟mysql擴(kuò)展(一般情況下是開啟的);

檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。

php鏈接代碼如下:

?php

//設(shè)置編碼格式

header("Content-type:text/html;charset=utf-8");

//定義數(shù)據(jù)庫主機(jī)地址

$host="localhost";

//定義mysql數(shù)據(jù)庫登錄用戶名

$user="root";

//定義mysql數(shù)據(jù)庫登錄密碼

$pwd="";

//鏈接數(shù)據(jù)庫

$conn = mysql_connect($host,$user,$pwd);

//對連接進(jìn)行判斷

if(!$conn){

die("數(shù)據(jù)庫連接失??!".mysql_errno());

}else{

echo "數(shù)據(jù)庫連接成功!";

}

?

php怎么連接數(shù)據(jù)庫

直接寫代碼啊。

我寫了一遍截圖看。第一行參數(shù)主機(jī)、用戶名、密碼;第二行選擇數(shù)據(jù)庫‘第三行選擇字符集’

你自己試下

當(dāng)前題目:php連接數(shù)據(jù)庫及調(diào)用 php怎么和mysql數(shù)據(jù)庫連接
URL網(wǎng)址:http://muchs.cn/article44/hgsdee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、商城網(wǎng)站、電子商務(wù)網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、做網(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)

h5響應(yīng)式網(wǎng)站建設(shè)