新聞中心
php怎么連接數(shù)據(jù)庫(kù)
直接寫代碼啊。
創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè),為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),高端網(wǎng)站設(shè)計(jì),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
我寫了一遍截圖看。第一行參數(shù)主機(jī)、用戶名、密碼;第二行選擇數(shù)據(jù)庫(kù)‘第三行選擇字符集’
你自己試下
怎么將php與數(shù)據(jù)庫(kù)連接
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫(kù);
檢查php環(huán)境是否已開啟mysql擴(kuò)展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項(xiàng);b.打開php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫(kù)主機(jī)地址
$host="localhost";
//定義mysql數(shù)據(jù)庫(kù)登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫(kù)登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫(kù)
$conn = mysql_connect($host,$user,$pwd);
//對(duì)連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫(kù)連接失??!".mysql_errno());
}else{
echo "數(shù)據(jù)庫(kù)連接成功!";
}
?
php操作MYSQL數(shù)據(jù)庫(kù)的步驟是什么?
1.鏈接數(shù)據(jù)庫(kù)所在的服務(wù)器 mysql_connect 如:mysql_connect("127.0.0.1","root","111111") or die("未能鏈接上");
2.選擇數(shù)據(jù)庫(kù) mysql_select _db($db) or die("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤!")
3.建立資源標(biāo)識(shí)符 $r= mysql_query($sql)
4.讀出 mysql_fetch_rows($r),mysql_fetch_assoc($r)
如:if(mysql_num_rows($r)0){
while($rows=mysql_fetch_assoc($r)){
echo $rows["id"]."-".$rows["name"];
}
}
5.釋放資源 mysql_query($r);
PHP網(wǎng)站怎么連接到數(shù)據(jù)庫(kù)?
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實(shí)都是為了PHP解析XML來做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對(duì)于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請(qǐng)按照此標(biāo)準(zhǔn)順序書寫.其實(shí)也無(wú)所謂了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作為解析XML配置文件必備工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {
$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內(nèi)容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點(diǎn),進(jìn)而獲取相關(guān)的數(shù)據(jù)庫(kù)信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(guān)聯(lián)數(shù)組,方便接下來的方法調(diào)用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫(kù)配置文件信息出錯(cuò)!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫(kù)連接池
對(duì)于PHP程序而言,優(yōu)化永無(wú)止境。而數(shù)據(jù)庫(kù)連接池就在一定程度上起到了優(yōu)化的作用。其使得對(duì)用戶的每一個(gè)請(qǐng)求而言,無(wú)需每次都像數(shù)據(jù)庫(kù)申請(qǐng)鏈接資源。而是通過已存在的數(shù)據(jù)庫(kù)連接池中的鏈接來返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。
于是,這里簡(jiǎn)單的模擬了一下數(shù)據(jù)庫(kù)連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫(kù) 工具類設(shè)計(jì)
* ?郭璞
* ?2016年12月23日
*
**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無(wú)法進(jìn)行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫(kù)連接池“偽隊(duì)列”
$this-poolsize = $poolsize;
$this-dbpool = array (); ? ? ? ?for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark連接數(shù)據(jù)庫(kù)失?。?markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫(kù)連接池中獲取一個(gè)數(shù)據(jù)庫(kù)鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池中已無(wú)鏈接資源,請(qǐng)稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫(kù)鏈接資源放回到數(shù)據(jù)庫(kù)連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫(kù)連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
PHP訪問MySQL數(shù)據(jù)庫(kù)的步驟。
PHP訪問MySQL數(shù)據(jù)庫(kù):
因?yàn)檫B接數(shù)據(jù)庫(kù)需要較長(zhǎng)的時(shí)間和較大的資源開銷,所以如果在多個(gè)網(wǎng)頁(yè)中都要頻繁地訪問數(shù)據(jù)庫(kù),則可以建立與數(shù)據(jù)庫(kù)的持續(xù)連接。即調(diào)用mysql_pconnect()代替mysql_connect()。
基本步驟:
1.連接服務(wù)器:mysql_connect();
2.選擇數(shù)據(jù)庫(kù):mysql_select_db();
3.執(zhí)行SQL語(yǔ)句:mysql_query();
查詢:select
顯示:show
插入:insert into
更新:update
刪除:delete
4.關(guān)閉結(jié)果集:mysql_free_result($result);
5.關(guān)閉數(shù)據(jù)庫(kù):mysql_close($link);
PHP怎么連接MySQL
PHP連接mysql數(shù)據(jù)庫(kù)是PHP新手們必須要掌握的一項(xiàng)技能,只要掌握了PHP對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查等操作,就可以寫出一些簡(jiǎn)單且常見的程序。如留言表,新聞頁(yè)等。本篇文章主要給大家詳細(xì)介紹PHP連接Mysql數(shù)據(jù)庫(kù)的兩種常用方法。
下面我們通過具體的代碼示例來給大家詳細(xì)介紹兩種PHP連接mysql數(shù)據(jù)庫(kù)的方法。
mysqli連接數(shù)據(jù)庫(kù)和pdo連接數(shù)據(jù)庫(kù)。
第一種方法:使用mysqli連接mysql數(shù)據(jù)庫(kù)
代碼實(shí)例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$link=new mysqli($host,$user,$password,$dbName);
if ($link-connect_error){
die("連接失?。?.$link-connect_error);
}
$sql="select * from admins";
$res=$link-query($sql);
$data=$res-fetch_all();
var_dump($data);
在經(jīng)過一系列的連接操作后,我們?cè)賱?chuàng)建一個(gè)sql語(yǔ)句對(duì)其中數(shù)據(jù)表進(jìn)行查詢檢驗(yàn)。在上述代碼中,我們要先創(chuàng)建一些需要用到的變量,如數(shù)據(jù)庫(kù)用戶名、數(shù)據(jù)庫(kù)名密碼等。然后我們用面向?qū)ο蟮姆绞竭B接了名為php的數(shù)據(jù)庫(kù)。再通過if條件語(yǔ)句,connect-error方法判斷PHP連接數(shù)據(jù)庫(kù)是否成功。
這里我們先登錄phpmyadmin看看是否存在php數(shù)據(jù)庫(kù),從下圖可以知道是存在php這個(gè)數(shù)據(jù)庫(kù)的。
最后通過瀏覽器訪問,結(jié)果如下圖:
從圖中可以得知,我們成功地連接了php數(shù)據(jù)庫(kù),并且能查詢出數(shù)據(jù)表信息。
第二種方法:使用PDO連接數(shù)據(jù)庫(kù)
代碼示例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$pdo=new PDO("mysql:host=$host;dbname=$dbName",$user,$password);
$sql="select * from admins";
$data=$pdo-query($sql)-fetch();
var_dump($data);
PHP連接Mysql步驟以上就是關(guān)于PHP連接數(shù)據(jù)庫(kù)查詢數(shù)據(jù)的兩種常用方法詳解,更多相關(guān)教程請(qǐng)?jiān)L問php中文網(wǎng)mysql視頻教程,歡迎參考學(xué)習(xí)
本文標(biāo)題:php連接數(shù)據(jù)庫(kù)三大步 php怎么連接數(shù)據(jù)庫(kù)
文章出自:http://www.dlmjj.cn/article/hpcdep.html