提供已知的幾種連接方式:
創(chuàng)新互聯(lián)是一家專業(yè)提供化德企業(yè)網(wǎng)站建設,專注與網(wǎng)站設計、網(wǎng)站制作、H5網(wǎng)站設計、小程序制作等業(yè)務。10年已為化德眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
1.
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("test.mdb");
$conn-Open($connstr);
2.
$conn=new com("ADODB.connection");
$connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".realpath("test.mdb");
$conn-open($connstr);
3.
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
?php
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo $sql;
mysql_close($conn);
echo "Hello!數(shù)據(jù)庫mycounter已經(jīng)成功建立!";
1.最簡單的方式
?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_close($con);?
2.面向?qū)ο髆ysqli(詳細教程)
?php$mysqli = new mysqli('localhost','root','','volunteer');if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error();}?
3.pdo連接mysql(詳細教程)
?php $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');try { foreach ($db-query('select * from user') as $row){ print_r($row); } $db = null; //關閉數(shù)據(jù)庫} catch (PDOException $e) { echo $e-getMessage();}?
4.ADODB連接mysql(詳細教程)
?phprequire_once './adodb5/adodb.inc.php';$conn = ADONewConnection('mysql');$conn-connect('localhost','root','','test');$conn-Execute("set names utf8");$res = $conn-Execute("select * from user");if (!$res){ echo $conn-ErrorMsg();}else{ var_dump($res);}?
Oracle(甲骨文)是世界上最為流行的關系數(shù)據(jù)庫。它是大公司推崇的工業(yè)化的強有力的引擎。我們先看看其相關的函數(shù):
(1)integer
ora_logon(string
user
,
string
password)
開始對一個Oracle數(shù)據(jù)庫服務器的連接。
(2)integer
ora_open(integer
connection)
打開給出的連接的游標。
(3)integer
ora_do(integer
connection,
string
query)
在給出的連接上執(zhí)行查詢。PHP生成一個指示器,解析查詢,并執(zhí)行之。
(4)integer
ora_parse(integer
cursor,
string
query)
解析一個查詢并準備好執(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)
返回當前的值。列由零開始的數(shù)字索引。
(8)boolean
ora_logoff(integer
connection)
斷開對數(shù)據(jù)庫服務器的鏈接。
以下是向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
?
//先設置兩個環(huán)境變量ORACLE_HOME,ORACLE_SID
putenv("ORACLE_HOME=/oracle/app/oracle/product/8.0.4");
putenv("ORACLE_SID=ora8");
//設置網(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數(shù)據(jù)庫代碼
?php? ? $mysql_server_name= localhost ;? //改成自己的mysql數(shù)據(jù)庫服務器 ? $mysql_username= root ;? //改成自己的mysql數(shù)據(jù)庫用戶名 ? $mysql_password= ;? //改成自己的mysql數(shù)據(jù)庫密碼 ? $mysql_database= mycounter ; ?//改成自己的mysql數(shù)據(jù)庫名 ? $conn=mysql_connect($mysql_server_name $mysql_username $mysql_password $mysql_database);? ? $sql= CREATE?DATABASE?mycounter? DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;? ? ;? ? mysql_query($sql);? ? $sql= CREATE?TABLE?`counter`? (`id`?INT( )?UNSIGNED?NOT?NULL? AUTO_INCREMENT? `count`?INT( )? UNSIGNED?NOT?NULL?DEFAULT? PRIMARY?KEY? (?`id`?)?)?TYPE?=?innodb; ;? ? mysql_select_db($mysql_database $conn);? ? $result=mysql_query($sql);? ? //echo?$sql;? ? mysql_close($conn);? ? echo?"Hello!數(shù)據(jù)庫mycounter已經(jīng)成功建立!";? ? ?
PHP連接數(shù)據(jù)庫之PHP連接ACCESS數(shù)據(jù)庫代碼方法
? ? $conn?=?new?("ADODB Connection");? ? $connstr?=?"DRIVER={Microsoft?Access?Driver?(* mdb)};?DBQ=" ?realpath("data/db mdb");? ? $conn Open($connstr);? ? $rs?=?new?("ADODB RecordSet");? ? $rs Open("select?*?from?szd_t" $conn );? ? while(!?$rs eof)?{? ? $f?=?$rs Fields( );? ? echo?$f value;? ? $rs MoveNext();? ? }? ? ?
PHP連接數(shù)據(jù)庫之PHP連接MS SQL數(shù)據(jù)庫代碼方法
安裝SQL服務器并添加PHP的MSSQL擴展
使用以下代碼連接并測試
?php? ? $myServer?=?localhost;?//主機 ? $myUser?=?sa;?//用戶名 ? $myPass?=?password;?//密碼 ? $myDB?=?Northwind;?//MSSQL庫名 ? $s?=?@mssql_connect($myServer ?$myUser ?$myPass)? ? or?die(Couldnt?connect?to?SQL?Server?on?$myServer);? ? $d?=?@mssql_select_db($myDB ?$s)? ? or?die(Couldnt?open?database?$myDB);? ? $query?=?SELECT?TitleOfCourtesy+?+FirstName+?+LastName?AS?Employee?;? ? $query? =?FROM?Employees?;? ? $query? =?WHERECountry=USA?AND?Left(HomePhone ? )?=?( );? ? $result?=?mssql_query($query);? ? $numRows?=?mssql_num_rows($result);? ? echo? h ? ?$numRows? ?Row? ?($numRows?==? ???:?s)? ?Returned?/ h ;? ? while($row?=?mssql_fetch_array($result))? ? {? ? echo? li? ?$row[Employee]? ? /li;? ? }? ? ?
PHP連接數(shù)據(jù)庫之PHP連接Oracle數(shù)據(jù)庫
PHP提供了兩套函數(shù)與Oracle連接 分別是ORA_和OCI函數(shù) 其中ORA_函數(shù)略顯陳舊 OCI函數(shù)更新?lián)f更好一些 兩者的使用語法幾乎相差無幾 你的PHP安裝選項應該可以支持兩者的使用
?? if?($conn=Ora_Logon("user@TNSNAME" "password"))? ? {?echo?"SUCCESS?!?Connected?to?databasen";? ? }else? ? {echo?"Failed?: (?Could?not?connect?to?databasen";}? ? Ora_Logoff($conn);? ? phpinfo();? ? ?? ? lishixinzhi/Article/program/PHP/201405/30761