PHP連接數(shù)據(jù)庫之PHP連接MYSQL數(shù)據(jù)庫代碼
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)與策劃設(shè)計,林口網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:林口等地區(qū)。林口做網(wǎng)站價格咨詢:13518219792
?php? ? $mysql_server_name= localhost ;? //改成自己的mysql數(shù)據(jù)庫服務(wù)器 ? $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服務(wù)器并添加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安裝選項應(yīng)該可以支持兩者的使用
?? 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
?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)成功建立!";
PHP鏈接數(shù)據(jù)庫有幾種方式
mysqli:
?php?
$servername?=?"localhost";?
$username?=?"username";?
$password?=?"password";?
//?創(chuàng)建連接?
$conn?=?new?mysqli($servername,?$username,?$password);?
//?檢測連接?
if?($conn-connect_error)?{
die("連接失敗:?"?.?$conn-connect_error);?
}?
echo?"連接成功";?
?
也可以使用PDO進行鏈接,前提是你必須在php.ini中開啟PDO:
?php
$servername?=?"localhost";
$username?=?"username";
$password?=?"password";
try?{
$conn?=?new?PDO("mysql:host=$servername;dbname=myDB",?$username,?$password);
echo?"連接成功";?
}
catch(PDOException?$e)
{
echo?$e-getMessage();
}
?
建議使用PDO,功能更加強大,兼容各種數(shù)據(jù)庫