php連接數(shù)據(jù)庫(kù)的類 php連接數(shù)據(jù)庫(kù)的語(yǔ)句

php 連接數(shù)據(jù)庫(kù)類

我也剛剛學(xué)PHP,正在研究中,雖然你只給10分........

10年的平山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整平山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“平山網(wǎng)站設(shè)計(jì)”,“平山網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

首先,將代碼保存到一個(gè)文件,如:mysql.class.php

其次,在一個(gè)常用的文件里調(diào)用:比如頭部文件header.php,因?yàn)槲曳旁诟夸浰杂孟旅娣绞綄?dǎo)入其他文件:

require dirname(__FILE__) . 'include/config.php';

//導(dǎo)入類文件

require dirname(__FILE__) . 'include/mysql.class.php';

//定義一個(gè)類及初始化數(shù)據(jù)庫(kù)類

$db = new mysql($db_host, $db_user, $db_pass, $db_name);

$db_host = $db_user = $db_pass = $db_name = NULL;

然后,在test.php文件調(diào)用:

require_once dirname(__FILE__) . '/header.php';

使用方法:

$sql = "讀取表";

$res = $db-query($sql);

$info = array();//定義數(shù)組

while($row=$db-fetchRow($res))

{

$arr['id'] = $row['id'];

$arr['title'] = $row['title'];

$info[] = $arr;

}

可以在顯示的地方用:

foreach($info as $i)

{

echo $i['title']."br /";

}

或是直接使用while

還用另一種調(diào)用方式:

$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");

$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);

測(cè)試通過(guò),因?yàn)槲艺谑褂?....................................

config.php代碼:

?php

$db_host = "localhost";

$db_name = "test";

$db_user = "root";

$db_pass = "";

$table = "mini_";

$charset = "gb2312";

$dbcharset = "gbk";

?

mysql.class.php代碼:

?php

class mysql

{

var $link = NULL;

//自動(dòng)執(zhí)行__construct php5類構(gòu)建方法,如果PHP4和PHP5同時(shí)使用會(huì)自動(dòng)使用PHP5的方法

function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

//自動(dòng)執(zhí)行時(shí)調(diào)用mysql函數(shù)

$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);

}

//php4類構(gòu)建方法,如果沒(méi)有 __construct 就自動(dòng)執(zhí)行此功能

function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

if ($quiet)

{

$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);

}

else

{

$this-settings = array(

'dbhost' = $dbhost,

'dbuser' = $dbuser,

'dbpw' = $dbpw,

'dbname' = $dbname,

'charset' = $charset,

'pconnect' = $pconnect

);

}

}

function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)

{

global $dbcharset;

if ($pconnect)

{

if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))

{

if (!$quiet)

{

$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");

}

return false;

}

}

else

{

if (PHP_VERSION = '4.2')

{

$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);

}

else

{

$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);

mt_srand((double)microtime() * 1000000);

}

if (!$this-link)

{

if (!$quiet)

{

$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");

}

return false;

}

}

$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);

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

if ($this-version '4.1')

{

if ($dbcharset != 'latin1')

{

mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);

}

if ($this-version '5.0.1')

{

mysql_query("SET sql_mode=''", $this-link);

}

}

if ($dbname)

{

if (mysql_select_db($dbname, $this-link) === false )

{

if (!$quiet)

{

$this-ErrorMsg("Can't select MySQL database($dbname)!");

}

return false;

}

else

{

return true;

}

}

else

{

return true;

}

}

function query($sql, $type = '')

{

if ($this-link === NULL)

{

$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);

$this-settings = array();

}

if ($this-queryCount++ = 99)

{

$this-queryLog[] = $sql;

}

if ($this-queryTime == '')

{

if (PHP_VERSION = '5.0.0')

{

$this-queryTime = microtime(true);

}

else

{

$this-queryTime = microtime();

}

}

if (!($query = mysql_query($sql, $this-link)) $type != 'SILENT')

{

$this-error_message[]['message'] = 'MySQL Query Error';

$this-error_message[]['sql'] = $sql;

$this-error_message[]['error'] = mysql_error($this-link);

$this-error_message[]['errno'] = mysql_errno($this-link);

$this-ErrorMsg();

return false;

}

return $query;

}

function affected_rows()

{

return mysql_affected_rows($this-link);

}

function num_fields($query)

{

return mysql_num_fields($query);

}

function error()

{

return mysql_error($this-link);

}

function errno()

{

return mysql_errno($this-link);

}

function num_rows($query)

{

return mysql_num_rows($query);

}

function insert_id()

{

return mysql_insert_id($this-link);

}

function fetchRow($query)

{

return mysql_fetch_assoc($query);

}

function fetcharray($query)

{

return mysql_fetch_array($query);

}

function version()

{

return $this-version;

}

function close()

{

return mysql_close($this-link);

}

function ErrorMsg($message = '', $sql = '')

{

if ($message)

{

echo "$message\n\n";

}

else

{

echo "bMySQL server error report:";

print_r($this-error_message);

}

exit;

}

function getCol($sql)

{

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

if ($res !== false)

{

$arr = array();

while ($row = mysql_fetch_row($res))

{

$arr[] = $row[0];

}

return $arr;

}

else

{

return false;

}

}

function getOne($sql, $limited = false)

{

if ($limited == true)

{

$sql = trim($sql . ' LIMIT 1');

}

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

if ($res !== false)

{

$row = mysql_fetch_row($res);

if ($row !== false)

{

return $row[0];

}

else

{

return '';

}

}

else

{

return false;

}

}

function getAll($sql)

{

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

if ($res !== false)

{

$arr = array();

while ($row = mysql_fetch_assoc($res))

{

$arr[] = $row;

}

return $arr;

}

else

{

return false;

}

}

//使用: getRow($sql,true) 如果有true那值是 limit 1,讀取一條信息

function getRow($sql, $limited = false)

{

if ($limited == true)

{

$sql = trim($sql . ' LIMIT 1');

}

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

if ($res !== false)

{

return mysql_fetch_assoc($res);

}

else

{

return false;

}

}

}

?

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來(lái)做準(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ū)寫(xiě).其實(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ù)組,方便接下來(lái)的方法調(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)鏈接資源。而是通過(guò)已存在的數(shù)據(jù)庫(kù)連接池中的鏈接來(lái)返回,從時(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連接數(shù)據(jù)庫(kù)的類,但是選擇數(shù)據(jù)庫(kù)的時(shí)候總是出錯(cuò),怎么回事呢?

構(gòu)造函數(shù)錯(cuò)咯

function?__construct($host,$user,$pass,$database){

$this?-?host=$host;

$this?-?user=$user;

$this?-?pass=$pass;

$this?-?database=$database;

//?echo?$db;

$conn?=?mysql_connect($host,$user,$pass);

$db?=?mysql_select_db($this?-?database,$conn);

if($db){

echo?"數(shù)據(jù)庫(kù)成功";

}else{

echo?"數(shù)據(jù)庫(kù)失敗";

}

}

//給你改咯哈這個(gè)類

class?register{

private?$host;????????//The?host?address

private?$user;????????//The?user

private?$pass;????//The?password

private?$database;??????????//The?database

private?$conn;

//Connect?with?the?database

function?__construct($host,$user,$pass,$database){

$this?-?host=$host;

$this?-?user=$user;

$this?-?pass=$pass;

$this?-?database=$database;??????????

$conn?=?mysql_connect($host,$user,$pass)or?die("連接失敗!");

mysql_select_db($this?-?database,$conn)?or?die("選擇數(shù)據(jù)庫(kù)失敗!");

$this-conn=$conn;

}

//Add?a?user

function?addUser($data){

$dataObj????=?????$data;//類里邊不要把變量寫(xiě)得太死

$userInfo???=?????json_decode($dataObj,true);??//change?the?data?from?the?type?of?json?to?array.

$username???=?????$userInfo[0];

$password???=?????$userInfo[1];

$repassword?=?????$userInfo[2];

//The?sql?to?add?the?new?user

@$sql_addUser?=?EOF

insert?into?users?values("","$username","$password","$repassword");

EOF;

//?var_dump($sql_addUser);break;

$result?=?mysql_query($sql_addUser,$this-conn);//指定連接,少些麻煩

//?var_dump($result);break;

if($result){

echo??1;

}else{

echo?0;

}

}

}

網(wǎng)頁(yè)標(biāo)題:php連接數(shù)據(jù)庫(kù)的類 php連接數(shù)據(jù)庫(kù)的語(yǔ)句
網(wǎng)站URL:http://muchs.cn/article32/ddceppc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google網(wǎng)站策劃、網(wǎng)站排名、建站公司、移動(dòng)網(wǎng)站建設(shè)App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)