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

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

php數(shù)據(jù)庫連接接口 php連接數(shù)據(jù)庫有什么用

pdo是什么意思?

PDO(PHP Data Objects)是一種在PHP里連接數(shù)據(jù)庫的使用接口。PDO與mysqli曾經(jīng)被建議用來取代原本PHP在用的mysql相關(guān)函數(shù),基于數(shù)據(jù)庫使用的安全性,因為后者欠缺對于SQL注入的防護。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鹽山免費建站歡迎大家使用!

PHP 數(shù)據(jù)對象(PDO) 擴展為PHP訪問數(shù)據(jù)庫定義了一個輕量級的一致接口。實現(xiàn) PDO 接口的每個數(shù)據(jù)庫驅(qū)動可以公開具體數(shù)據(jù)庫的特性作為標準擴展功能。 注意利用 PDO 擴展自身并不能實現(xiàn)任何數(shù)據(jù)庫功能;必須使用一個具體數(shù)據(jù)庫的 PDO 驅(qū)動來訪問數(shù)據(jù)庫服務(wù)。

相關(guān)信息:

PDO 提供了一個數(shù)據(jù)訪問抽象層,這意味著,不管使用哪種數(shù)據(jù)庫,都可以用相同的函數(shù)(方法)來查詢和獲取數(shù)據(jù)。 PDO不提供數(shù)據(jù)庫抽象層;它不會重寫 SQL,也不會模擬缺失的特性。如果需要的話,應(yīng)該使用一個成熟的抽象層。

從 PHP 5.1開始附帶了 PDO,在 PHP 5.0 中是作為一個 PECL 擴展使用。 PDO 需要PHP 5核心的新OO特性,因此不能在較早版本的 PHP 上運行。

怎么將php與數(shù)據(jù)庫連接

php鏈接mysql必備條件:

已安裝mysql數(shù)據(jù)庫;

檢查php環(huán)境是否已開啟mysql擴展(一般情況下是開啟的);

檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。

php鏈接代碼如下:

?php

//設(shè)置編碼格式

header("Content-type:text/html;charset=utf-8");

//定義數(shù)據(jù)庫主機地址

$host="localhost";

//定義mysql數(shù)據(jù)庫登錄用戶名

$user="root";

//定義mysql數(shù)據(jù)庫登錄密碼

$pwd="";

//鏈接數(shù)據(jù)庫

$conn = mysql_connect($host,$user,$pwd);

//對連接進行判斷

if(!$conn){

die("數(shù)據(jù)庫連接失?。?.mysql_errno());

}else{

echo "數(shù)據(jù)庫連接成功!";

}

?

php中怎么把數(shù)據(jù)庫連接寫成一個接口

我自己封裝的一個

?php

class AppConfig{

public static $dbParam = array(

'dbHost' = 'localhost',

'dbUser' = 'root',

'dbPassword' ='',

'dbName' = '數(shù)據(jù)庫名',

'dbCharset' = 'utf8',

'dbPort' = 3306,

'dbPrefix' = 'test_',

'dbPconnect' = 0,

'dbDebug' = true,

);

}

class Model {

private $version = ''; //mysql版本

private $config = array(); //數(shù)據(jù)庫配置數(shù)組

private $class; //當前類名

public $tablepre = 'ts_'; //表前綴

public $db = ''; //庫名

public $table = ''; //表名

private static $link; //數(shù)據(jù)庫鏈接句柄

private $data = array(); //中間數(shù)據(jù)容器

private $condition = ''; //查詢條件

private $fields = array(); //字段信息

private $sql = array(); //sql集合,調(diào)試用

public $primaryKey = 'id'; //表主鍵

//構(gòu)造函數(shù)初始化

public function __construct($dbParam = array()) {

$this-config = (is_array($dbParam) !empty($dbParam)) ? $dbParam : AppConfig::$dbParam;

$this-connect();

$this-init();

}

//鏈接數(shù)據(jù)庫

private function connect() {

if($this-config['dbPconnect']) {

self::$link = @mysql_pconnect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword']);

}else{

self::$link = @mysql_connect($this-config['dbHost'], $this-config['dbUser'], $this-config['dbPassword'], true);

}

mysql_errno(self::$link) != 0 $this-errdie('Could not connect Mysql: ');

$this-db= !empty($this-db) ? $this-db : $this-config['dbName'];

$serverinfo = $this-version();

if ($serverinfo '4.1' $this-config['dbCharset']) {

mysql_query("SET character_set_connection=".$this-config['dbCharset'].",character_set_results=".$this-config['dbCharset'].",character_set_client=binary", self::$link);

}

if ($serverinfo '5.0') {

mysql_query("SET sql_mode=''", self::$link);

}

@mysql_select_db($this-db, self::$link) or $this-errdie('Cannot use database');

return self::$link;

}

//表基本信息初始化

protected function init() {

$this-class = get_class($this);

$this-table = !empty($this-table) ? $this-table : strtolower($this-class);

$this-table = $this-tablepre . $this-table;

return $this;

}

//設(shè)置屬性值

public function __set($name, $value) {

//exit($value);

$this-data['fields'][$name] = $value;

}

//獲取屬性值

public function __get($name) {

if(isset($this-data['fields'][$name])) {

return($this-data['fields'][$name]);

}else {

return NULL;

}

}

//字段信息處理

private function implodefields($data) {

if (!is_array($data)) {

$data = array();

}

$this-fields = !empty($this-data['fields']) ? array_merge($this-data['fields'], $data) : $data;

foreach($this-fields as $key = $value) {

$fieldsNameValueStr[] = "`$key`='$value'";

$fieldsNameStr[] = "`$key`";

$fieldsValueStr[] = "'$value'";

}

return array($fieldsNameValueStr, $fieldsNameStr, $fieldsValueStr);

}

//條件判斷組裝

private function condition($where = NULL) {

if (is_numeric($where)) {

$where = "WHERE `{$this-primaryKey}`='{$where}' LIMIT 1";

}elseif (is_array($where)){

$where = "WHERE `{$this-primaryKey}` in (".implode(',',$where).")";

}elseif(!empty($this-data['condition'])){

//'預(yù)留WHERE', 'order', 'group', 'limit' …………等條件關(guān)鍵詞處理接口

$where = $where ? "WHERE {$where}" : "WHERE 1";

isset($this-data['condition']['where']) $where .= ' AND '.$this-data['condition']['where'];

isset($this-data['condition']['group']) $where .= ' GROUP BY '.$this-data['condition']['group'];

isset($this-data['condition']['order']) $where .= ' ORDER BY '.$this-data['condition']['order'];

isset($this-data['condition']['limit']) $where .= ' LIMIT '.$this-data['condition']['limit'];

}else{

$where = "WHERE {$where}";

}

$this-condition = $where;

return $this;

}

//插入數(shù)據(jù)

public function insert($data = array(), $replace = false) {

$fields = $this-implodefields($data);

$insert = $replace ? 'REPLACE' : 'INSERT';

$sql = "{$insert} INTO `{$this-db}`.`{$this-table}` (".implode(', ',$fields[1]).") values (".implode(', ',$fields[2]).")";

$this-query($sql);

return $this-getInsertId();

}

//更新數(shù)據(jù)

public function update($data = array() ,$where = '') {

$numargs = func_num_args();

if ($numargs == 1) {

$where = $data;

$data = array();

}

$fields = $this-implodefields($data);

$this-condition($where);

$sql = "UPDATE `{$this-db}`.`{$this-table}` SET ".implode(', ',$fields[0])." {$this-condition}";

$this-query($sql);

return $this-getAffectedRows();

}

//刪除數(shù)據(jù)

public function delete($where = NULL) {

if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'delete'){

$sql = $where;

}else{

$this-condition($where);

$sql = "DELETE FROM `{$this-db}`.`{$this-table}` {$this-condition}";

}

$this-query($sql);

return $this-getAffectedRows();

}

//查詢數(shù)據(jù)

public function select($where = NULL, $fields = '*') {

if(!is_array($where) strtolower(substr(trim($where), 0, 6)) == 'select'){

$sql = $where;

}else{

$this-condition($where);

$sql = "SELECT {$fields} FROM `{$this-db}`.`{$this-table}` {$this-condition}";

}

return $this-fetch($this-query($sql));

}

//查詢一條數(shù)據(jù)

public function getOne($where, $fields = '*') {

$data = $this-select($where, $fields = '*');

if($data) {

return $data[0];

}

return array();

}

//查詢多條數(shù)據(jù)

public function getAll($where, $fields = '*') {

$data = $this-select($where, $fields = '*');

return $data;

}

//結(jié)果數(shù)量

public function getCount($where = '', $fields = '*') {

$this-condition($where);

$sql = "SELECT count({$fields}) as count FROM `{$this-db}`.`{$this-table}` {$this-condition}";

$data = $this-query($sql);

if($data){

return @mysql_result($data,0);

}

return 0;

}

//執(zhí)行sql語句(flag為0返回mysql_query查詢后的結(jié)果,為1返回lastid,其他返回影響行數(shù),默認為2返回影響行數(shù))

public function query($sql, $flag = '0', $type = '') {

if ($this-config['dbDebug']) {

$startime = $this-microtime_float();

}

//查詢

if ($type == 'UNBUFFERED' function_exists('mysql_unbuffered_query')) {

$result = @mysql_unbuffered_query($sql, self::$link);

} else {

//exit($sql);

$result = @mysql_query($sql, self::$link);

}

//重試

if (in_array(mysql_errno(self::$link), array(2006,2013)) empty($result) $this-config['dbPconnect']==0 !defined('RETRY')) {

define('RETRY',true); @mysql_close(self::$link); sleep(2);

$this-connect();

$result = $this-query($sql);

}

if ($result === false) {

$this-errdie($sql);

}

if ($this-config['dbDebug']) {

$endtime = $this-microtime_float();

$this-sql[] = array($sql,$endtime-$startime);

}

//清空操作數(shù)據(jù)

$this-data = array();

return $flag == '0' ? $result : ($flag == '1' ? $this-getInsertId() : $this-getAffectedRows());

}

//返回結(jié)果$onlyone為true返回一條否則返回所有,$type有MYSQL_ASSOC,MYSQL_NUM,MYSQL_BOTH

public function fetch($result, $onlyone = false, $type = MYSQL_ASSOC) {

if($result){

if ($onlyone) {

$row = @mysql_fetch_array($result, $type);

return $row;

}else{

$rowsRs = array();

while($row=@mysql_fetch_array($result, $type)) {

$rowsRs[] = $row;

}

return $rowsRs;

}

}

return array();

}

//可以運行SELECT,SHOW,EXPLAIN 或 DESCRIBE 等返回一個資源標識符的語句得到返回結(jié)果數(shù)組

public function show($sql, $onlyone = false) {

return $this-fetch($this-query($sql), $onlyone);

}

// 使用call函數(shù)處理同類型函數(shù)

private function __call($name, $arguments) {

$callArr = array('on', 'where', 'order', 'between', 'group', 'limit');

if (in_array($name, $callArr)) {

$this-data['condition'][$name] = $arguments[0];

}else{

$this-errdie("function error: function {$name} is not in ($this-class) class exist");

}

return $this;

}

//返回最后一次插入ID

public function getInsertId() {

return @mysql_insert_id(self::$link);

}

//返回受影響行數(shù)

public function getAffectedRows() {

return @mysql_affected_rows(self::$link);

}

//獲取錯誤信息

private function error() {

return ((self::$link) ? @mysql_error(self::$link) : @mysql_error());

}

//獲取錯誤信息ID

private function errno() {

return ((self::$link) ? @mysql_errno(self::$link) : @mysql_errno());

}

//獲取版本信息

function version() {

if(empty($this-version)) {

$this-version = mysql_get_server_info(self::$link);

}

return $this-version;

}

//打印錯誤信息

private function errdie($sql = '') {

if ($this-config['dbDebug']) {

die('/BRBMySQL ERROR/B/BR

SQL:'.$sql.'/BR

ERRNO:'.$this-errno().'/BR

ERROR:'.$this-error().'/BR');

}

die('DB ERROR!?。?);

}

//獲取時間微妙數(shù)

private function microtime_float()

{

list($usec, $sec) = explode(" ", microtime());

return ((float)$usec + (float)$sec);

}

//析構(gòu)函數(shù)

public function __destruct() {

echo 'hr';

$this-config['dbDebug'] print_r($this-sql);

//unset($this-result);

//unset($this-condition);

//unset($this-data);

}

}

class user extends Model {

//public $db = 'qsf_mvc';

//public $table = 'user';

public $primaryKey = 'uid';

}

$userObj = new user();

//---------------------------------------插入數(shù)據(jù)方法一-----------------------------------------

//模擬ActiveRecord模式 插入數(shù)據(jù)

$userObj-username = 'hoho';

$userObj-passwd = '1478522';

$userObj-email = 'qsf.z11@163.com';

$userObj-sex = 1;

$userObj-desc = '清潔工';

$insetId = $userObj-insert();

if ($insetId 0) {

echo "插入ID為:{$insetId}BR";

}

//---------------------------------------插入數(shù)據(jù)方法二-----------------------------------------

//直接數(shù)組做參數(shù)插入數(shù)據(jù)

$userArr = array(

'username' = 'hoho',

'passwd' = '1478522',

'email' = 'qsf.z2121ia@163.com',

'sex' = '1',

'desc' = '廚師',

);

$insetId = $userObj-insert($userArr);

if ($insetId 0) {

echo "插入ID為:{$insetId}BR";

}

//---------------------------------------更新數(shù)據(jù)方法一----------------------------------------

$userObj-username = 'h111oho';

$userObj-passwd = '1478511122';

$userObj-email = 'qsf111ia@163.com';

$userObj-sex = 1;

$userObj-desc = '清潔工';

$affectedRows1 = $userObj-update(89);

if ($affectedRows1 0) {

echo "影響行數(shù)為:{$affectedRows1}BR";

}

//---------------------------------------更新數(shù)據(jù)方法二----------------------------------------

//更新記錄(傳遞參數(shù)的方式和insert操作一樣)

$userArr = array(

'username' = 'hohoho',

'passwd' = '1474rr4448522',

'email' = 'qsf.rrza@165.com',

'sex' = '0',

'desc' = '廚師qq',

);

$affectedRows = $userObj-update($userArr, $insetId);

if ($affectedRows 0) {

echo "影響行數(shù)為:{$affectedRows}BR";

}

//----------------------------------------查詢數(shù)據(jù)----------------------------------------------

$userRs0 = $userObj-select(8); //單個主鍵值

//print_r($userRs0);

$userRs1 = $userObj-select(array(1,5,8)); //多個主鍵值的數(shù)組

//print_r($userRs1);

$userRs2 = $userObj-select('select count(*) as count from user where uid 20'); //直接完整sql語句

//print_r($userRs2);

$userRs3 = $userObj-select("`uid` 0"); //where條件

//print_r($userRs3);

$userRs4 = $userObj-getOne("`uid` 0"); //獲取單條記錄

//print_r($userRs4);

$usersRs5 = $userObj-getAll("`uid` 0"); ////獲取所有記錄

//print_r($usersRs5);

$usersRs6 = $userObj-limit('0,10')-where('uid 100')-order('uid DESC')-group('username')-select();

//print_r($usersRs6);

//----------------------------------------刪除數(shù)據(jù)-----------------------------------------------

//刪除操作傳遞參數(shù)的方式和select操作一樣

$userObj-delete(60); //單個主鍵值

$userObj-delete(array(1,5,8)); //多個主鍵值的數(shù)組

$userObj-delete('delete from user where uid 100'); //直接完整sql語句

$userObj-delete("`uid` 100"); //where條件

$userObj-limit('5')-where('uid 80')-delete();

//----------------------------------------特殊查詢-----------------------------------------------

$userShowRs = $userObj-show('show create table user', true); //獲取特殊查詢的結(jié)果,第二個參數(shù)代表返回一條結(jié)果還是所有的結(jié)果

PHP網(wǎng)站怎么連接到數(shù)據(jù)庫?

常規(guī)方式

常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。

// 讀取配置文件內(nèi)容

$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123

PHP解析XML

上述兩種讀取文件,其實都是為了PHP解析XML來做準備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。

配置文件

?xml version="1.0" encoding="UTF-8" ?mysql

!-- 為防止出現(xiàn)意外,請按照此標準順序書寫.其實也無所謂了 --

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é)點,進而獲取相關(guān)的數(shù)據(jù)庫信息

$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點信息賦值給關(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ù)庫配置文件信息出錯!/markbr /" );

} ? ? ? ?return $dbconfig;

}

}1234567891011121314151617181920212223242526272829

數(shù)據(jù)庫連接池

對于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對用戶的每一個請求而言,無需每次都像數(shù)據(jù)庫申請鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時間上,效率上,都是一個大大的提升。

于是,這里簡單的模擬了一下數(shù)據(jù)庫連接池的實現(xiàn)。核心在于維護一個“池”。

從池子中取,用畢,歸還給池子。

?php/**x

* ?PHP中的數(shù)據(jù)庫 工具類設(shè)計

* ?郭璞

* ?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文件丟失,無法進行配置文件的初始化操作!/markbr /" );

}else {

require './utils.php';

} ? ? ? ?// 初始化 配置文件信息

$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準備好數(shù)據(jù)庫連接池“偽隊列”

$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ù)庫失??!/markbr /" );

array_push ( $this-dbpool, $conn );

}

} ? ?/**

* 從數(shù)據(jù)庫連接池中獲取一個數(shù)據(jù)庫鏈接資源

*

* @throws ErrorException

* @return mixed

*/

public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池中已無鏈接資源,請稍后重試!/mark" );

} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );

}

} ? ?/**

* 將用完的數(shù)據(jù)庫鏈接資源放回到數(shù)據(jù)庫連接池

*

* @param unknown $conn

* @throws ErrorException

*/

public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池已滿/markbr /" );

} else {

array_push ( $this-dbpool, $conn );

}

}

}


當前標題:php數(shù)據(jù)庫連接接口 php連接數(shù)據(jù)庫有什么用
本文來源:http://weahome.cn/article/doshghd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部