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

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

php封裝數(shù)據(jù)庫(kù)連接類,php接口封裝

php封裝一個(gè)class類,實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)的增刪改查怎么操做?

class sqlHelper{ \x0d\x0a public $conn; \x0d\x0a public $dbname="數(shù)據(jù)庫(kù)名稱"; \x0d\x0a public $username="數(shù)據(jù)庫(kù)用戶名"; \x0d\x0a public $password="數(shù)據(jù)庫(kù)密碼"; \x0d\x0a public $host="localhost"; \x0d\x0a //連接數(shù)據(jù)庫(kù) \x0d\x0a public function __construct(){ \x0d\x0a $this-conn=mysql_connect($this-host,$this-username,$this-password); \x0d\x0a if(!$this-conn){ \x0d\x0a die("連接失敗".mysql_error()); \x0d\x0a } \x0d\x0a mysql_select_db($this-dbname,$this-conn); \x0d\x0a } \x0d\x0a //執(zhí)行查詢語(yǔ)句 \x0d\x0a public function execute_dql($sql){ \x0d\x0a $res=mysql_query($sql,$this-conn); \x0d\x0a return $res; \x0d\x0a } \x0d\x0a //執(zhí)行增填改語(yǔ)句 \x0d\x0a public function execute_dml($sql){ \x0d\x0a $b=mysql_query($sql,$this-conn); \x0d\x0a if(!$b){ \x0d\x0a return 3; \x0d\x0a }else{ \x0d\x0a if(mysql_affected_rows($this-conn)){ \x0d\x0a return 1;//表示OK \x0d\x0a }else{ \x0d\x0a return 2;//表示沒(méi)有行收到影響 \x0d\x0a } \x0d\x0a } \x0d\x0a }\x0d\x0a}

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供梅里斯企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為梅里斯眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。

PHP訪問(wèn)MYSQL數(shù)據(jù)庫(kù)封裝類(附函數(shù)說(shuō)明)

復(fù)制代碼

代碼如下:

?php

/*

MYSQL

數(shù)據(jù)庫(kù)訪問(wèn)封裝類

MYSQL

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

訪問(wèn)方式,本封裝類以mysql_封裝

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

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

mysql_connect

or

mysql_pconnect

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

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)前頁(yè)面進(jìn)程查詢數(shù)據(jù)庫(kù)的次數(shù)

var

$dblink

;

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

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

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("無(wú)法鏈接數(shù)據(jù)庫(kù)!");

}

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

mysql_query("SET

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

;

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

$dbname

@mysql_select_db($dbname,$this-dblink)

;

}

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

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ù)目,只對(duì)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,只對(duì)表有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)

;

}

//輸出簡(jiǎn)單的錯(cuò)誤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ù)庫(kù)出錯(cuò):".htmlspecialchars($msg)."\n"

;

$message

.=

"/body\n"

;

$message

.=

"/html"

;

echo

$message

;

exit

;

}

}

?

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

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

首先,將代碼保存到一個(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封裝數(shù)據(jù)庫(kù)連接類,php接口封裝
本文URL:http://weahome.cn/article/heeeeg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部