php訪問數(shù)據(jù)庫類 php數(shù)據(jù)庫操作類

PHP訪問MYSQL數(shù)據(jù)庫封裝類(附函數(shù)說明)

復(fù)制代碼

網(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

/*

MYSQL

數(shù)據(jù)庫訪問封裝類

MYSQL

數(shù)據(jù)訪問方式,php4支持以mysql_開頭的過程訪問方式,php5開始支持以mysqli_開頭的過程和mysqli面向?qū)ο?/p>

訪問方式,本封裝類以mysql_封裝

數(shù)據(jù)訪問的一般流程:

1,連接數(shù)據(jù)庫

mysql_connect

or

mysql_pconnect

2,選擇數(shù)據(jù)庫

mysql_select_db

3,執(zhí)行SQL查詢

mysql_query

4,處理返回的數(shù)據(jù)

mysql_fetch_array

mysql_num_rows

mysql_fetch_assoc

mysql_fetch_row

etc

*/

class

db_mysql

{

var

$querynum

=

;

//當(dāng)前頁面進程查詢數(shù)據(jù)庫的次數(shù)

var

$dblink

;

//數(shù)據(jù)庫連接資源

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

function

connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0

,

$halt=true)

{

$func

=

empty($pconnect)

?

'mysql_connect'

:

'mysql_pconnect'

;

$this-dblink

=

@$func($dbhost,$dbuser,$dbpw)

;

if

($halt

!$this-dblink)

{

$this-halt("無法鏈接數(shù)據(jù)庫!");

}

//設(shè)置查詢字符集

mysql_query("SET

character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this-dblink)

;

//選擇數(shù)據(jù)庫

$dbname

@mysql_select_db($dbname,$this-dblink)

;

}

//選擇數(shù)據(jù)庫

function

select_db($dbname)

{

return

mysql_select_db($dbname,$this-dblink);

}

//執(zhí)行SQL查詢

function

query($sql)

{

$this-querynum++

;

return

mysql_query($sql,$this-dblink)

;

}

//返回最近一次與連接句柄關(guān)聯(lián)的INSERT,UPDATE

或DELETE

查詢所影響的記錄行數(shù)

function

affected_rows()

{

return

mysql_affected_rows($this-dblink)

;

}

//取得結(jié)果集中行的數(shù)目,只對select查詢的結(jié)果集有效

function

num_rows($result)

{

return

mysql_num_rows($result)

;

}

//獲得單格的查詢結(jié)果

function

result($result,$row=0)

{

return

mysql_result($result,$row)

;

}

//取得上一步

INSERT

操作產(chǎn)生的

ID,只對表有AUTO_INCREMENT

ID的操作有效

function

insert_id()

{

return

($id

=

mysql_insert_id($this-dblink))

=

?

$id

:

$this-result($this-query("SELECT

last_insert_id()"),

0);

}

//從結(jié)果集提取當(dāng)前行,以數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回

function

fetch_row($result)

{

return

mysql_fetch_row($result)

;

}

//從結(jié)果集提取當(dāng)前行,以字段名為key表示的關(guān)聯(lián)數(shù)組形式返回

function

fetch_assoc($result)

{

return

mysql_fetch_assoc($result);

}

//從結(jié)果集提取當(dāng)前行,以字段名和數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回

function

fetch_array($result)

{

return

mysql_fetch_array($result);

}

//關(guān)閉鏈接

function

close()

{

return

mysql_close($this-dblink)

;

}

//輸出簡單的錯誤html提示信息并終止程序

function

halt($msg)

{

$message

=

"html\nhead\n"

;

$message

.=

"meta

content='text/html;charset=gb2312'\n"

;

$message

.=

"/head\n"

;

$message

.=

"body\n"

;

$message

.=

"數(shù)據(jù)庫出錯:".htmlspecialchars($msg)."\n"

;

$message

.=

"/body\n"

;

$message

.=

"/html"

;

echo

$message

;

exit

;

}

}

?

PHP用戶類的一個方法怎樣調(diào)用數(shù)據(jù)庫操作類

直接調(diào)用就行了,不過可能你需要引用文件,以下是例子

//文件conn.php,用于連接數(shù)據(jù)庫

class

DB_Conn

{

}

//文件

db.php,

用于數(shù)據(jù)庫操作,這個類必然需要使用數(shù)據(jù)庫連接對象,因此引用conn.php

require_once

conn.php;

class

DB

{

}

//文件user.php

require_once

'db.php';

class

User

{

public

function

getUserById($id)

{

$conn

=

new

Db_Conn();

$db

=

new

Db();

}

}以上只是示意,如果文件不在一個目錄下記得修改路徑。而且,一般來說數(shù)據(jù)庫對象應(yīng)該包含連接數(shù)據(jù)庫和數(shù)據(jù)操作的全部功能,不需要分別寫在兩個類里面。我覺得你對面向?qū)ο蟮睦斫膺€很淺薄,需要進一步累積經(jīng)驗。

PHP采用pdo方式訪問數(shù)據(jù)庫時,exec方法和prepare方法有什么區(qū)別

區(qū)別是:

PHP采用pdo方式訪問數(shù)據(jù)庫時,您不必再使用再為它們封裝數(shù)據(jù)庫操作類,只需要使用PDO接口中的方法就可以對各種數(shù)據(jù)庫進行操作。

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

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

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

其次,在一個常用的文件里調(diào)用:比如頭部文件header.php,因為我放在根目錄所以用下面方式導(dǎo)入其他文件:

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

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

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

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

$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']);

測試通過,因為我正在使用.....................................

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;

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

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

{

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

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

}

//php4類構(gòu)建方法,如果沒有 __construct 就自動執(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訪問數(shù)據(jù)庫類 php數(shù)據(jù)庫操作類
本文URL:http://muchs.cn/article24/docdjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、建站公司、網(wǎng)站制作、響應(yīng)式網(wǎng)站、小程序開發(fā)移動網(wǎng)站建設(shè)

廣告

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

商城網(wǎng)站建設(shè)