PHP+MYSQL操作类
2、WEB页面编码???? (1)PHP文件的编码也要非常注意。本例中使用的“UTF8”。所以我拿PHP文件用记事本打开是“ANSI”格式的,我就把它另存为“UTF8”格式的。如果是其他格式,就存为相应该的编码文件。不然页面会出现乱码。
????
???? (2)PHP页面的编码可以用HTML标签指定,在charset后加入自己的编码;
????
??
???? 也可以在PHP脚本中使用。
???
mysql_query("set names utf8",$this->conn);?
??? 如果数据库使用的GBK,或GB2312,则换成相应的编码。
???
?
[php] view plaincopyprint?
- <?php??class?DBOperate{??
- ????var?$host;?? ????var?$user;??
- ????var?$password;?? ????var?$conn;??
- ????var?$db;?? ????/*?
- ????*构造函数?????*@param?$host?主机名?
- ????*@param?$user?数据库用户?????*@param?$password?数据库密码?
- ????*@param?$db?当前使用的数据库名?????*/??
- ????function?DBOperate($host,$user,$password,$db){?? ????????$this->host=$host;??
- ????????$this->user=$user;?? ????????$this->password=$password;??
- ????????$this->db=$db;?? ????????$this->conn=mysql_connect($this->host,$this->user,$this->password)??
- ????????????????????or?die("connect?error:".mysql_error());?? ????????mysql_select_db($this->db,$this->conn)??
- ???????????or?die("switch?db?error:".mysql_error());?? ????????mysql_query("set?names?utf8",$this->conn);??
- ????}??????/*?
- ????*@param?$sql?添加记录的语句?????*用于插入记录?
- ????*/??????function?Add($sql){??
- ????????mysql_query($sql,$this->conn)?or?die("insert?error:".mysql_error());?? ????}??
- ????/*?????*@param?$sql?更新记录的语句?
- ????*用于更新记录?????*/??
- ????function?Update($sql){?? ????????mysql_query($sql,$this->conn)?or?die("update?error:".mysql_error());??
- ????}??????/*?
- ????*@param?$sql?删除记录的语句?????*用于删除记录?
- ????*/??????function?Delete($sql){??
- ????????mysql_query($sql,$this->conn)?or?die("delete?error:".mysql_error());?? ????}??
- ????/*?????*@param?$sql?查询记录的语句?
- ????*@return?$arrs?以一个数组的形式返回数据库中所有记录的结果集?????*结果如下:Array([0]=>Array(第一条记录)?[1]=>Array(第二条记录)...)?
- ????**/??????function?getRecords($sql){??
- ????????$all=mysql_query($sql,$this->conn);?? ????????$i=0;??
- ????????while($result=mysql_fetch_array($all)){?? ????????????$arrs[$i]=$result;??
- ????????????$i++;?? ????????}??
- ????????return?$arrs;?? ????}??
- ????/*?????*@param?$sql?查询记录的语句?
- ????*@return?$arrs?以一个数组的形式返回所有字段的结果集?????*结果如下:Array([0]=>字段名?[1]=>字段名...)?
- ????**/??????function?getFields($sql){??
- ????????$all=mysql_query($sql,$this->conn);?? ????????$i=0;??
- ????????while($result=mysql_fetch_field($all)){?? ????????????$arrs[$i]=$result->name;??
- ????????????$i++;?? ????????}??
- ????????return?$arrs;?? ????}??
- ????function?Close(){?? ????????mysql_close($this->conn);??
- ????}??}??
- $db=new?DBOperate("localhost","root","admin","mydb");?? //$db->Add("insert?into?news?values(null,'新闻依旧','无上智者',now())"); ??
- print_r($db->getRecords("select?*?from?news"));?? $result=$db->getFields("select?*?from?news");??
- for($i=0;$i<count($result);$i++){?? ????echo?$result[$i]."<br?/>";??
- }??$db->Close();??
- ?>??