真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

php怎么用sql數(shù)據(jù)庫 php操作mysql數(shù)據(jù)庫

如何用php連接sql server數(shù)據(jù)庫?

我用的PHP版本是php5.6.12,mssql數(shù)據(jù)庫版本為2008

創(chuàng)新互聯(lián)建站憑借在網(wǎng)站建設(shè)、網(wǎng)站推廣領(lǐng)域領(lǐng)先的技術(shù)能力和多年的行業(yè)經(jīng)驗,為客戶提供超值的營銷型網(wǎng)站建設(shè)服務(wù),我們始終認為:好的營銷型網(wǎng)站就是好的業(yè)務(wù)員。我們已成功為企業(yè)單位、個人等客戶提供了成都網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),以良好的商業(yè)信譽,完善的服務(wù)及深厚的技術(shù)力量處于同行領(lǐng)先地位。

兩種途徑:

1、直接連接

$servname="CAPTAINHERO567";

$conninfo=array(

"Database"="netdata",

"UID"="lxz2005",

"PWD"="831140");

$conn=sqlsrv_connect($servname,

$conninfo);

$sql="select

*

from

Pinfo";

$db=sqlsrv_query($conn,

$sql);

while($row=sqlsrv_fetch_array($db))

{

echo("

".iconv("GB2312","UTF-8",$row["Pname"])."

");

}

2、使用PDO抽象數(shù)據(jù)層連接

$hostname

=

"192.168.1.100";

$dbname

=

"Northwind";

$username

=

"sa";

$pwd

=

"pwd100";

$dsn="sqlsrv:Server=$hostname;database=$dbname";

$conn

=

new

PDO

($dsn,$username,$pwd);

$conn-setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

echo

"mssql

database

connnection

sucessed!";

php怎么連接mysql數(shù)據(jù)庫并導(dǎo)入數(shù)據(jù)?

方法/步驟

1

登錄到phpMyAdmin

2

新建一個要導(dǎo)入的數(shù)據(jù)庫,點擊“+new”

3

如圖,分別填寫數(shù)據(jù)庫名稱,以及選擇數(shù)據(jù)庫的排序規(guī)則,

4

完成步驟3,點擊“創(chuàng)建”

5

完成步驟4,從右側(cè)就可以看到我們新創(chuàng)建的數(shù)據(jù)庫了,如果沒有立即顯示,刷新即可立馬顯示了。

6

單擊我們新創(chuàng)建的數(shù)據(jù)庫,

7

然后,我們選擇“導(dǎo)入”,

8

完成步驟7,我們點擊“選擇文件”,

9

點擊了“選擇文件”之后,就會出現(xiàn)如圖所示的彈出框,我們選擇要導(dǎo)入的sql就可以了,后綴名可以是.sql,也可以是壓縮文件.zip。如圖,選擇好文件之后,點擊“確定”就可以了

10

完成步驟⑨,你就可以從剛才的頁面中看到自己上傳的文件了,如圖所示,其余選項默認就可以了,然后點擊“執(zhí)行”就可以了。

11

這個時候,你就可以看到你的數(shù)據(jù)已經(jīng)導(dǎo)入到你新創(chuàng)建的數(shù)據(jù)庫中了

如何利用PHP執(zhí)行.SQL文件

本篇文章是對使用PHP執(zhí)行 SQL文件的實現(xiàn)代碼進行了詳細的分析介紹 需要的朋友參考下 ?

demo php:

復(fù)制代碼 代碼如下: ?php /** * 讀取 sql 文件并寫入數(shù)據(jù)庫 * @version demo php */ class DBManager { ??? var $dbHost = ; ??? var $dbUser = ; ??? var $dbPassword = ; ??? var $dbSchema = ; ??? function __construct($host $user $password $schema) ??? { ??????? $this dbHost = $host; ??????? $this dbUser = $user; ??????? $this dbPassword = $password; ??????? $this dbSchema = $schema; ??? } ??? function createFromFile($sqlPath $delimiter = (;/n)|((;/r/n))|(;/r) $prefix = $menter = array( # )) ??? { ??????? //判斷文件是否存在 ??????? if(!file_exists($sqlPath)) ??????????? return false; ??????? $handle = fopen($sqlPath rb );?? ??????? $sqlStr = fread($handle filesize($sqlPath)); ??????? //通過sql語法的語句分割符進行分割 ??????? $segment = explode(";" trim($sqlStr)); ??????? //var_dump($segment); ??????? //去掉注釋和多余的空行 ??????? foreach($segment as $statement) ??????? { ??????????? $sentence = explode("/n" $statement); ??????????? $newStatement = array(); ??????????? foreach($sentence as $subSentence) ??????????? { ??????????????? if( != trim($subSentence)) ??????????????? { ??????????????????? //判斷是會否是注釋 ??????????????????? $isComment = false; ??????????????????? foreach($menter as $er) ??????????????????? { ??????????????????????? if(eregi("^(" $er ")" trim($subSentence))) ??????????????????????? { ??????????????????????????? $isComment = true; ??????????????????????????? break; ??????????????????????? } ??????????????????? } ??????????????????? //如果不是注釋 則認為是sql語句 ??????????????????? if(!$isComment) ??????????????????????? $newStatement[] = $subSentence;?????????????????? ??????????????? } ??????????? } ??????????? $statement = $newStatement; ??????? } ??????? //對表名加前綴 ??????? if( != $prefix) ??????? { ?????? ??????????? //只有表名在第一行出現(xiàn)時才有效 例如 CREATE TABLE talbeName ??????????? $regxTable = "^[/`/ /"]{ }[/_a zA Z]+[/_a zA Z ]*[/`/ /"]{ }$";//處理表名的正則表達式 ??????????? $regxLeftWall = "^[/`/ /"]{ }"; ??????????? $sqlFlagTree = array( ??????????????????? "CREATE" = array( ??????????????????????????? "TABLE" = array( ??????????????????????????????????? "$regxTable" = ??????????????????????????????? ) ??????????????????????? ) ??????????????????? "INSERT" = array( ??????????????????????????? "INTO" = array( ??????????????????????????????? "$regxTable" = ??????????????????????????? ) ??????????????????????? ) ??????????????????? ); ??????????? foreach($segment as $statement) ??????????? { ??????????????? $tokens = split(" " $statement[ ]); ??????????????? $tableName = array(); ??????????????? $this findTableName($sqlFlagTree $tokens $tableName); ??????????????? if(empty($tableName[ leftWall ])) ??????????????? { ??????????????????? $newTableName = $prefix $tableName[ name ]; ??????????????? } ??????????????? else{ ??????????????????? $newTableName = $tableName[ leftWall ] $prefix substr($tableName[ name ] ); ??????????????? } ??????????????? $statement[ ] = str_replace($tableName[ name ] $newTableName $statement[ ]); ??????????? } ??????? }?????? ??????? //組合sql語句 ??????? foreach($segment as $statement) ??????? { ??????????? $newStmt = ; ??????????? foreach($statement as $sentence) ??????????? { ??????????????? $newStmt = $newStmt trim($sentence) "/n"; ??????????? } ??????????? $statement = $newStmt; ??????? } ??????? //用于測試 ?????? ??????? //var_dump($segment); ??????? //writeArrayToFile( data txt $segment); ??????? // ??????? self::saveByQuery($segment); ??????? return true; ??? } ??? private function saveByQuery($sqlArray) ??? { ??????? $conn = mysql_connect($this dbHost $this dbUser $this dbPassword); ??????? mysql_select_db($this dbSchema); ??????? foreach($sqlArray as $sql) ??????? { ??????????? mysql_query($sql); ??????? }?????? ??????? mysql_close($conn); ??? } ??? private function findTableName($sqlFlagTree $tokens $tokensKey= $tableName = array()) ??? { ??????? $regxLeftWall = "^[/`/ /"]{ }"; ??????? if(count($tokens)=$tokensKey) ??????????? return false;?????? ??????? if( == trim($tokens[$tokensKey])) ??????? { ??????????? return self::findTableName($sqlFlagTree $tokens $tokensKey+ $tableName); ??????? } ??????? else ??????? { ??????????? foreach($sqlFlagTree as $flag = $v) ??????????? {?? ??????????????? if(eregi($flag $tokens[$tokensKey])) ??????????????? { ??????????????????? if( ==$v) ??????????????????? { ??????????????????????? $tableName[ name ] = $tokens[$tokensKey]; ??????????????????????? if(eregi($regxLeftWall $tableName[ name ])) ??????????????????????? { ??????????????????????????? $tableName[ leftWall ] = $tableName[ name ]{ }; ??????????????????????? } ??????????????????????? return true; ??????????????????? } ??????????????????? else{ ??????????????????????? return self::findTableName($v $tokens $tokensKey+ $tableName); ??????????????????? } ??????????????? } ??????????? } ??????? } ??????? return false; ??? } } function writeArrayToFile($fileName $dataArray $delimiter="/r/n") { ??? $handle=fopen($fileName "wb"); ??? $text = ; ??? foreach($dataArray as $data) ??? { ??????? $text = $text $data $delimiter; ??? } ??? fwrite($handle $text); } //測試 $dbM = new DBManager( localhost w f test ); $dbM createFromFile( data sql null fff_ ); ? lishixinzhi/Article/program/PHP/201311/21281

怎樣用PHP連接sql數(shù)據(jù)庫

1、學(xué)習(xí)的方法,最好是看手冊

在前一篇文章中涉及到了連接數(shù)據(jù)庫sqlsrv_connect();還記得我們mysql連接數(shù)據(jù)庫的時候也是mysql_connect();兩者操作數(shù)據(jù)庫是很相似的。從零基礎(chǔ)開始學(xué)起的話,只能是查閱手冊。查看一下sqlsrv有哪些函數(shù)提供我們使用!推薦網(wǎng)址是:

【注意】很多的方法都跟mysql提供的方法很相似,看到后面的函數(shù)名就大概知道里面的用法,比如說mysql執(zhí)行sql語句的時候調(diào)用mysql_query();而sqlserver執(zhí)行sql語句的時候調(diào)用也是sqlsrv_query(),但是特別注意一點就是,它們的傳遞參數(shù)不一樣。詳細的只能看一下手冊。接下來我簡單總結(jié)一下操作數(shù)據(jù)的方法

2、連接數(shù)據(jù)庫sqlsrv_connect()

?php$serverName = "serverName\sqlexpress";//服務(wù)器的名字,本地localhost$connectionInfo = array( "Database"="dbName", "UID"="userName", "PWD"="password");$conn = sqlsrv_connect( $serverName, $connectionInfo);if( $conn ) { ? ? echo "Connection established.br /";

}else{ ? ? echo "Connection could not be established.br /"; ? ? die( print_r( sqlsrv_errors(), true));

}

3、操作數(shù)據(jù)庫

1)執(zhí)行sql語句sqlsrv_query(),返回值為true或者false,這里函數(shù)的用法跟mysql_query(),不一樣。它需要把連接的資源句柄當(dāng)作參數(shù)傳進去,看源碼。資源句柄就是上面代碼連接數(shù)據(jù)庫的“$conn”。

$sql = "select * from test1"; ? //sql語句$data = sqlsrv_query($conn,$sql); ?//$conn資源句柄if($data == true){ ? ?die("執(zhí)行成功");

}else{ ? ?die("執(zhí)行失敗");

}

2)獲取結(jié)果集

//以數(shù)值索引數(shù)組、關(guān)聯(lián)數(shù)組或這兩種數(shù)組的形式檢索下一行的數(shù)據(jù)。類似于mysql_fetch_arraysqlsrv_fetch_array ?

//以對象形式檢索下一行的數(shù)據(jù)。sqlsrv_fetch_object 1234

$sql = "select * from test1";$data = sqlsrv_query($conn,$sql);if($data == true){ ? ?while($row = sqlsrv_fetch_array( $data, SQLSRV_FETCH_ASSOC) ) { ? ? ? ? ?echo $row['id'].", ".$row['name']."br /";

}else{ ? ? ? ? die( print_r( sqlsrv_errors(), true));

}

}

$sql = "SELECT fName, lName FROM Table_1";$stmt = sqlsrv_query( $conn, $sql);if( $stmt === false ) { ? ? die( print_r( sqlsrv_errors(), true));

}while( $obj = sqlsrv_fetch_object( $stmt)) { ? ? ?echo $obj-fName.", ".$obj-lName."br /";

}

3)顯示錯誤信息sqlsrv_errors():上面都有用到這個函數(shù),只要是操作數(shù)據(jù)庫發(fā)生錯誤,都可以使用這個函數(shù)打印出來看一下壓

這里就不用上代碼了


新聞名稱:php怎么用sql數(shù)據(jù)庫 php操作mysql數(shù)據(jù)庫
路徑分享:http://weahome.cn/article/ddchesd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部