1 <?php 2 3 //最终的单例的数据库操作类 4 5 final class Db 6 7 { 8 9 //私有的静态的保存对象的属性 10 11 private static $obj = NULL; 12 13 //数据库配置信息属性 14 15 private $db_host;//主机名 16 17 private $db_user;//用户名 18 19 private $db_pass;//密码 20 21 private $db_name;//数据库 22 23 private $charset;//字符集 24 25 26 27 //私有的构造方法:阻止类外new对象 28 29 private function __construct($config) 30 31 { 32 33 $this->db_host = $config[‘db_host‘]; 34 35 $this->db_user = $config[‘db_user‘]; 36 37 $this->db_pass = $config[‘db_pass‘]; 38 39 $this->db_name = $config[‘db_name‘]; 40 41 $this->charset = $config[‘charset‘]; 42 43 $this->connMySQL(); //连接数据库 44 45 $this->selectDb(); //选择数据库 46 47 $this->setCharacter(); //设置字符集 48 49 } 50 51 52 53 //私有的克隆方法:阻止类外clone对象 54 55 private function __clone(){} 56 57 //公共的静态的创建对象的方法 58 59 public static function getInstance($config) 60 61 { 62 63 //判断对象是否存在,如果不存在,则创建 64 65 if(!self::$obj instanceof self) 66 67 { 68 69 //如果对象不存在,则创建 70 71 self::$obj = new self($config); 72 73 } 74 75 return self::$obj;//返回对象 76 77 } 78 79 80 81 //私有的连接数据库的方法 82 83 private function connMySQL() 84 85 { 86 87 $link = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); 88 89 if(!$link) 90 91 { 92 93 exit("PHP连接MySQL失败!"); 94 95 } 96 97 } 98 99 100 101 //私有的选择数据库的方法 102 103 private function selectDb() 104 105 { 106 107 if(!mysql_select_db($this->db_name)) 108 109 { 110 111 exit("选择数据库{$this->db_name}失败!"); 112 113 } 114 115 } 116 117 118 119 //私有的设置数据库字符集 120 121 private function setCharacter() 122 123 { 124 125 return $this->exec("set names {$this->charset}"); 126 127 } 128 129 130 131 //执行SQL语句的方法:insert、update、delete、set 132 133 public function exec($sql = NULL) 134 135 { 136 137 //$sql = "SELECT * FROM student" 138 139 //SQL语句转成全小写 140 141 $sql = strtolower($sql); 142 143 //如果是SELECT语句,中止执行 144 145 if(substr($sql,0,6)=="select") 146 147 { 148 149 exit("SELECT语句不能调用该方法!"); 150 151 } 152 153 //如果非SELECT语句 154 155 return mysql_query($sql); 156 157 } 158 159 160 161 //私有的执行SQL语句的方法:select 162 163 private function query($sql = NULL) 164 165 { 166 167 //SQL语句转成全小写 168 169 $sql = strtolower($sql); 170 171 //如果是非SELECT语句,中止执行 172 173 if(substr($sql,0,6)!="select") 174 175 { 176 177 exit("非SELECT语句不能调用此方法!"); 178 179 } 180 181 //如果是SELECT语句,直接执行,返回结果集 182 183 return mysql_query($sql); 184 185 } 186 187 188 189 //公共的返回多行数据的方法 190 191 public function fetchAll($sql,$type=3) 192 193 { 194 195 //返回的数组的常量类型 196 197 $types = array( 198 199 1 => MYSQL_NUM, 200 201 2 => MYSQL_BOTH, 202 203 3 => MYSQL_ASSOC 204 205 ); 206 207 208 209 //执行SQL语句,并返回结果集 210 211 $result = $this->query($sql); 212 213 214 215 //循环取出结果集中所有行数据,并构建二维数组返回 216 217 while($row = mysql_fetch_array($result,$types[$type])) 218 219 { 220 221 $arr[] = $row; 222 223 } 224 225 226 227 //返回二维数组 228 229 return $arr; 230 231 } 232 233 234 235 //析构方法 236 237 public function __destruct() 238 239 { 240 241 //关闭当前的数据库连接 242 243 mysql_close(); 244 245 } 246 247 } 248 249 ?>
1 <?php 2 3 //(0)定义类的自动加载函数 4 5 function __autoload($className) 6 7 { 8 9 $filename = "./libs/$className.class.php"; 10 11 require_once($filename); 12 13 } 14 15 //(1)数据库配置信息 16 17 $arr = array( 18 19 ‘db_host‘ => ‘localhost‘, 20 21 ‘db_user‘ => ‘root‘, 22 23 ‘db_pass‘ => ‘root‘, 24 25 ‘db_name‘ => ‘student‘, 26 27 ‘charset‘ => ‘utf8‘ 28 29 ); 30 31 //(2)创建数据库对象 32 33 $db = Db::getInstance($arr); 34 35 //(3)遍历数组或对象的函数 36 37 function dump($arr){ 38 39 echo "<pre>"; 40 41 print_r($arr); 42 43 echo "</pre>"; 44 45 } 46 47 ?>
<?php header("content-type:text/html;charset=utf-8"); //(1)包含连接数据库的文件 require_once("./conn.php"); //(2)构建查询的SQL语句 $sql = "SELECT * FROM student ORDER BY id DESC"; //(3)取回所有行数据 $arr = $db->fetchAll($sql); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML5 Document</title> </head> <body> <div style="text-align:center;padding-bottom:20px;"> <h2>学生信息管理中心</h2> <a href="javascript:void(0)">添加学生</a> | 共有250个学生! </div> <table width="600" border="1" bordercolor="#ccc" rules="all" align="center" cellpadding="5"> <tr bgcolor="#f0f0f0"> <th>编号</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>学历</th> <th>工资</th> <th>奖金</th> <th>籍贯</th> <th>操作选项</th> </tr> <?php for($i=0;$i<count($arr);$i++){ ?> <tr align="center"> <td><?php echo $arr[$i][‘id‘]?></td> <td><?php echo $arr[$i][‘name‘]?></td> <td><?php echo $arr[$i][‘sex‘]?></td> <td><?php echo $arr[$i][‘age‘]?></td> <td><?php echo $arr[$i][‘edu‘]?></td> <td><?php echo $arr[$i][‘salary‘]?></td> <td><?php echo $arr[$i][‘bonus‘]?></td> <td><?php echo $arr[$i][‘city‘]?></td> <td> <a href="javascript:void(0)">修改</a> | <a href="javascript:void(0)">删除</a> </td> </tr> <?php }?> <tr> <td colspan="9">显示分页的代码</td> </tr> </table> </body> </html>
链接: http://pan.baidu.com/s/1hsdrZTU 密码: xm5d
原文:http://www.cnblogs.com/xnnhgz2016/p/6267042.html