PHP連接數據庫之PHP連接MYSQL數據庫代碼
< ?php $mysql_server_name=localhost; //改成自己的mysql數據庫服務器 $mysql_username=root; //改成自己的mysql數據庫用戶名 $mysql_password=; //改成自己的mysql數據庫密碼 $mysql_database=mycounter; //改成自己的mysql數據庫名 $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!數據庫mycounter已經成功建立!"; ?>PHP連接數據庫之PHP連接ACCESS數據庫代碼方法
< ? $conn = new com("ADODBConnection"); $connstr = "DRIVER={Microsoft
Access Driver (*mdb)};
DBQ=" realpath("data/dbmdb"); $conn>Open($connstr); $rs = new com("ADODBRecordSet"); $rs>Open("select *
from szd_t"$conn); while(! $rs>eof) { $f = $rs>Fields(); echo $f>value; $rs>MoveNext(); } ?>PHP連接數據庫之PHP連接MS SQL數據庫代碼方法
安裝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連接數據庫之PHP連接Oracle數據庫
PHP提供了兩套函數與Oracle連接分別是ORA_和OCI函數其中ORA_函數略顯陳舊OCI函數更新據說更好一些兩者的使用語法幾乎相差無幾你的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(); ?> 以上PHP連接數據庫的代碼使用TNSNAME(在你的tnsnamesora文件中指明)定義的Oracle數據庫名稱用戶名稱和密碼連接數據庫在成功連接的基礎上ora_logon函數返回一個非零的連接ID並儲存在變量$conn中
From:http://tw.wingwit.com/Article/program/PHP/201405/30761.html