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

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

phpmy數(shù)據(jù)庫封裝,mysql封裝

php實現(xiàn)mysql封裝類示例

php封裝mysql類

成都創(chuàng)新互聯(lián)公司的客戶來自各行各業(yè),為了共同目標,我們在工作上密切配合,從創(chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對我們的要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站制作、做網(wǎng)站、電商網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺開發(fā)。

復(fù)制代碼

代碼如下:

?php

class

Mysql

{

private

$host;

private

$user;

private

$pwd;

private

$dbName;

private

$charset;

private

$conn

=

null;

public

function

__construct()

{

$this-host

=

'localhost';

$this-user

=

'root';

$this-pwd

=

'root';

$this-dbName

=

'test';

$this-connect($this-host,$this-user,$this-pwd);

$this-switchDb($this-dbName);

$this-setChar($this-charset);

}

//負責(zé)鏈接

private

function

connect($h,$u,$p)

{

$conn

=

mysql_connect($h,$u,$p);

$this-conn

=

$conn;

}

//負責(zé)切換數(shù)據(jù)庫

public

function

switchDb($db)

{

$sql

=

'use'

.

$db;

$this-query($sql);

}

//負責(zé)設(shè)置字符集

public

function

setChar($char)

{

$sql

=

'set

names'

.

$char;

$this-query($sql);

}

//負責(zé)發(fā)送sql查詢

public

function

query($sql)

{

return

mysql_query($sql,$this-conn);

}

//負責(zé)獲取多行多列的select結(jié)果

public

function

getAll($sql)

{

$list

=

array();

$rs

=

$this-query($sql);

if

(!$rs)

{

return

false;

}

while

($row

=

mysql_fetch_assoc($rs))

{

$list[]

=

$row;

}

return

$list;

}

public

function

getRow($sql)

{

$rs

=

$this-query($sql);

if(!$rs)

{

return

false;

}

return

mysql_fetch_assoc($rs);

}

public

function

getOne($sql)

{

$rs

=

$this-query($sql);

if

(!$rs)

{

return

false;

}

return

mysql_fetch_assoc($rs);

return

$row[0];

}

public

function

close()

{

mysql_close($this-conn);

}

}

echo

'pre';

$mysql

=

new

Mysql();

print_r($mysql);

$sql

=

"insert

into

stu

values

(4,'wangwu','99998')";

if($mysql-query($sql)){

echo

"query成功";

}else

{

echo

"失敗";

}

echo

"br

/";

$sql

=

"select

*

from

stu";

$arr

=

$mysql-getAll($sql);

print_r($arr);

?

PHP網(wǎng)站MYSQL數(shù)據(jù)庫如何安裝及使用?

2. # chmod 755 mysql-standard-5.0.15-linux-gnu-i686-glibc23.tar.gz// 3. # tar xfz mysql-standard-5.0.15-linux-gnu-i686-glibc23.tar.gz //將解壓后生成的目錄,復(fù)制到/usr/local/下并改名為mysql 4. # groupadd mysql # useradd mysql -g mysql // 建立mysql組 //建立mysql用戶并且加入到mysql組中 5. # cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf 在support-files目錄下有4個模版文件,我們選擇其中一個座位Mysql的配置文件,覆蓋/etc/my.cnf(系統(tǒng)默認的配置,其中設(shè)置了性能參數(shù)和Mysql的一些路徑參數(shù)) 6. # cd /usr/local/mysql # ./scripts/mysql_install_db --user=mysql 進入mysql目錄 //初試化表并且規(guī)定用mysql用戶來訪問。初始化表以后就開始給mysql和root用戶設(shè)定訪問權(quán)限 7. # chown -R root . //設(shè)定root能訪問/usr/local/mysql 8. # chown -R mysql data //設(shè)定mysql用戶能訪問/usr/local/mysql/data ,里面存的是mysql的數(shù)據(jù)庫文件.這個目錄是在/etc/my.cnf中有配置,在mysql_install_db時產(chǎn)生。 9. # chown -R mysql data/. //設(shè)定mysql用戶能訪問/usr/local/mysql/data/mysql下的所有文件 10. # chgrp -R mysql . //設(shè)定mysql組能夠訪問/usr/local/mysql 11. # /usr/local/mysql/bin/mysqld_safe --user=mysql 運行mysql如果沒有問題的話,應(yīng)該會出現(xiàn)類似這樣的提示:[1] 42264# Starting mysqld daemon with databases from /usr/local/mysql/var 如果出現(xiàn) mysql ended這樣的語句,表示Mysql沒有正常啟動,你可以到log中查找問題,Log文件的通常在/etc/my.cnf中配置。大多數(shù)問題是權(quán)限設(shè)置不正確引起的。

php封裝一個class類實現(xiàn)mysql數(shù)據(jù)庫的增刪該查

?php

class db{

private $db;

const MYSQL_OPT_READ_TIMEOUT = 11;

const MYSQL_OPT_WRITE_TIMEOUT = 12;

private $tbl_name;

private $where;

private $sort;

private $fields;

private $limit;

public static $_instance = null;

function __construct(){

$cfg = loadConfig('db');

$db = mysqli_init();

$db-options(self::MYSQL_OPT_READ_TIMEOUT, 3);

$db-options(self::MYSQL_OPT_WRITE_TIMEOUT, 1);

@$db-real_connect($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);

if ($db-connect_error) {

$this-crash($db-errno,$db-error);

}

$db-set_charset("utf8");

$this-db = $db;

//echo $this-db-stat;

}

public static function getInstance(){

if(!(self::$_instance instanceof self)){

self::$_instance = new self();

}

return self::$_instance;

}

private function __clone() {} //覆蓋__clone()方法,禁止克隆

public function find($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql(),1);

}

public function findAll($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql());

}

//表

public function t($table){ $this-tbl_name = $table; return $this;}

//條件

public function where($conditions){

$where = '';

if(is_array($conditions)){

$join = array();

foreach( $conditions as $key = $condition ){

$condition = $this-db-real_escape_string($condition);

$join[] = "`{$key}` = '{$condition}'";

}

$where = "WHERE ".join(" AND ",$join);

}else{

if(null != $conditions) $where = "WHERE ".$conditions;

}

$this-where = $where;

return $this;

}

//排序

public function sort($sort){

if(null != $sort) $sort = "ORDER BY {$sort}";

$this-sort = $sort;

return $this;

}

//字段

public function fields($fields){ $this-fields = $fields; return $this; }

public function limit($limit){$this-limit = $limit; return $this;}

private function buildSql(){

$this-fields = empty($this-fields) ? "*" : $this-fields;

$sql = "SELECT {$this-fields} FROM {$this-tbl_name} {$this-where} {$this-sort}";

accessLog('db_access',$sql);

if(null != $this-limit)$sql .= " limit {$this-limit}";

return $sql;

}

/**

* 返回查詢數(shù)據(jù)

* @param $sql

* @param bool $hasOne

* @return array|bool|mixed

*/

private function getArray($sql,$hasOne = false){

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = "*";

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function findSql($sql,$hasOne = false){

accessLog('db_access',$sql);

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = "*";

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function create($row){

if(!is_array($row))return FALSE;

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$cols[] = '`'.$key.'`';

$vals[] = "'".$this-db-real_escape_string($value)."'";

}

$col = implode(',', $cols);

$val = implode(',', $vals);

$sql = "INSERT INTO `{$this-tbl_name}` ({$col}) VALUES ({$val})";

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當(dāng)前新增的ID

if($this-db-insert_id){

return $this-db-insert_id;

}

if($this-db-affected_rows){

return true;

}

}

return FALSE;

}

//直接執(zhí)行sql

public function runSql($sql){

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當(dāng)前新增的ID

return true;

}else{

return false;

}

}

public function update($row){

$where = "";

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$value = $this-db-real_escape_string($value);

$vals[] = "`{$key}` = '{$value}'";

}

$values = join(", ",$vals);

$sql = "UPDATE {$this-tbl_name} SET {$values} {$this-where}";

accessLog('db_access',$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當(dāng)前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return false;

}

function delete(){

$sql = "DELETE FROM {$this-tbl_name} {$this-where}";

if( FALSE != $this-db-query($sql) ){ // 獲取當(dāng)前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return FALSE;

}

private function prepera_format($rows){

$columns = $this-getArray("DESCRIBE {$this-tbl_name}");

$newcol = array();

foreach( $columns as $col ){

$newcol[$col['Field']] = $col['Field'];

}

return array_intersect_key($rows,$newcol);

}

//崩潰信息

private function crash($number,$message,$sql=''){

$msg = 'Db Error '.$number.':'.$message ;

if(empty($sql)){

echo t('db_crash');

}else{

$msg .= " SQL:".$sql;

echo t('db_query_err');

}

accessLog('db_error',$msg);

exit;

}

}

php的函數(shù)封裝如何插入到數(shù)據(jù)庫

本身這就是一個自定義函數(shù),數(shù)據(jù)需要插入的話需要自己構(gòu)造sql語句然后通過mysql_query將函數(shù)返回的值寫入數(shù)據(jù)庫。

想直接讓數(shù)據(jù)庫調(diào)用PHP的自定義函數(shù)是不現(xiàn)實的!

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

復(fù)制代碼

代碼如下:

?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

;

}

}

?


新聞標題:phpmy數(shù)據(jù)庫封裝,mysql封裝
文章出自:http://weahome.cn/article/dsipppj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部