php中mysql一次连接只能查询一次么_php单例模式(多次连接数据库只实例化一次)...

这篇博客介绍了一个PHP类db,实现了单例模式,确保数据库连接在整个应用中只被实例化一次。该类包含了select、insert、update和delete等方法,用于执行SQL查询,同时提供了getLastSql方法用于输出最后执行的SQL语句。
摘要由CSDN通过智能技术生成

1. <?php

2. class db {

3.     public $conn;

4.     public static $sql;

5.     public static $instance=null;

6.     private function __construct(){

7.         require_once('db.config.php');

8.         $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);

9.         if(!mysql_select_db($db['database'],$this->conn)){

10.            echo "失败";

11.        };

12.        mysql_query('set names utf8',$this->conn);

13.    }

14.    public static function getInstance(){

15.        if(is_null(self::$instance)){

16.            self::$instance = new db;

17.        }

18.        return self::$instance;

19.    }

20.    /**

21.     * 查询数据库

22.     */

23.    public function select($table,$condition=array(),$field = array()){

24.        $where='';

25.        if(!empty($condition)){

26.

27.            foreach($condition as $k=>$v){

28.                $where.=$k."='".$v."' and ";

29.            }

30.            $where='where '.$where .'1=1';

31.        }

32.        $fieldstr = '';

33.        if(!empty($field)){

34.

35.            foreach($field as $k=>$v){

36.                $fieldstr.= $v.',';

37.            }

38.             $fieldstr = rtrim($fieldstr,',');

39.        }else{

40.            $fieldstr = '*';

41.        }

42.        self::$sql = "select {$fieldstr} from {$table} {$where}";

43.        $result=mysql_query(self::$sql,$this->conn);

44.        $resuleRow = array();

45.        $i = 0;

46.        while($row=mysql_fetch_assoc($result)){

47.            foreach($row as $k=>$v){

48.                $resuleRow[$i][$k] = $v;

49.            }

50.            $i++;

51.        }

52.        return $resuleRow;

53.    }

54.    /**

55.     * 添加一条记录

56.     */

57.     public function insert($table,$data){

58.        $values = '';

59.        $datas = '';

60.        foreach($data as $k=>$v){

61.            $values.=$k.',';

62.            $datas.="'$v'".',';

63.        }

64.        $values = rtrim($values,',');

65.        $datas   = rtrim($datas,',');

66.        self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";

67.        if(mysql_query(self::$sql)){

68.            return mysql_insert_id();

69.        }else{

70.            return false;

71.        };

72.     }

73.     /**

74.      * 修改一条记录

75.      */

76.    public function update($table,$data,$condition=array()){

77.        $where='';

78.        if(!empty($condition)){

79.

80.            foreach($condition as $k=>$v){

81.                $where.=$k."='".$v."' and ";

82.            }

83.            $where='where '.$where .'1=1';

84.        }

85.        $updatastr = '';

86.        if(!empty($data)){

87.            foreach($data as $k=>$v){

88.                $updatastr.= $k."='".$v."',";

89.            }

90.            $updatastr = 'set '.rtrim($updatastr,',');

91.        }

92.        self::$sql = "update {$table} {$updatastr} {$where}";

93.        return mysql_query(self::$sql);

94.    }

95.    /**

96.     * 删除记录

97.     */

98.     public function delete($table,$condition){

99.        $where='';

100.        if(!empty($condition)){

101.

102.            foreach($condition as $k=>$v){

103.                $where.=$k."='".$v."' and ";

104.            }

105.            $where='where '.$where .'1=1';

106.        }

107.        self::$sql = "delete from {$table} {$where}";

108.        return mysql_query(self::$sql);

109.

110.     }

111.

112.    public static function getLastSql(){

113.        echo self::$sql;

114.    }

115.

116.

117.

118.}

119.

120.$db = db::getInstance();

121.//$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));

122.//echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));

123.//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));

124.echo $db->delete('demo',array('id'=>'2'));

125.db::getLastSql();

126.echo "

";

127.?>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值