首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

PHP+MYSQL操作种

2012-08-14 
PHP+MYSQL操作类2、WEB页面编码???? (1)PHP文件的编码也要非常注意。本例中使用的“UTF8”。所以我拿PHP文件用

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?
  1. <?php??class?DBOperate{??
  2. ????var?$host;?? ????var?$user;??
  3. ????var?$password;?? ????var?$conn;??
  4. ????var?$db;?? ????/*?
  5. ????*构造函数?????*@param?$host?主机名?
  6. ????*@param?$user?数据库用户?????*@param?$password?数据库密码?
  7. ????*@param?$db?当前使用的数据库名?????*/??
  8. ????function?DBOperate($host,$user,$password,$db){?? ????????$this->host=$host;??
  9. ????????$this->user=$user;?? ????????$this->password=$password;??
  10. ????????$this->db=$db;?? ????????$this->conn=mysql_connect($this->host,$this->user,$this->password)??
  11. ????????????????????or?die("connect?error:".mysql_error());?? ????????mysql_select_db($this->db,$this->conn)??
  12. ???????????or?die("switch?db?error:".mysql_error());?? ????????mysql_query("set?names?utf8",$this->conn);??
  13. ????}??????/*?
  14. ????*@param?$sql?添加记录的语句?????*用于插入记录?
  15. ????*/??????function?Add($sql){??
  16. ????????mysql_query($sql,$this->conn)?or?die("insert?error:".mysql_error());?? ????}??
  17. ????/*?????*@param?$sql?更新记录的语句?
  18. ????*用于更新记录?????*/??
  19. ????function?Update($sql){?? ????????mysql_query($sql,$this->conn)?or?die("update?error:".mysql_error());??
  20. ????}??????/*?
  21. ????*@param?$sql?删除记录的语句?????*用于删除记录?
  22. ????*/??????function?Delete($sql){??
  23. ????????mysql_query($sql,$this->conn)?or?die("delete?error:".mysql_error());?? ????}??
  24. ????/*?????*@param?$sql?查询记录的语句?
  25. ????*@return?$arrs?以一个数组的形式返回数据库中所有记录的结果集?????*结果如下:Array([0]=>Array(第一条记录)?[1]=>Array(第二条记录)...)?
  26. ????**/??????function?getRecords($sql){??
  27. ????????$all=mysql_query($sql,$this->conn);?? ????????$i=0;??
  28. ????????while($result=mysql_fetch_array($all)){?? ????????????$arrs[$i]=$result;??
  29. ????????????$i++;?? ????????}??
  30. ????????return?$arrs;?? ????}??
  31. ????/*?????*@param?$sql?查询记录的语句?
  32. ????*@return?$arrs?以一个数组的形式返回所有字段的结果集?????*结果如下:Array([0]=>字段名?[1]=>字段名...)?
  33. ????**/??????function?getFields($sql){??
  34. ????????$all=mysql_query($sql,$this->conn);?? ????????$i=0;??
  35. ????????while($result=mysql_fetch_field($all)){?? ????????????$arrs[$i]=$result->name;??
  36. ????????????$i++;?? ????????}??
  37. ????????return?$arrs;?? ????}??
  38. ????function?Close(){?? ????????mysql_close($this->conn);??
  39. ????}??}??
  40. $db=new?DBOperate("localhost","root","admin","mydb");?? //$db->Add("insert?into?news?values(null,'新闻依旧','无上智者',now())"); ??
  41. print_r($db->getRecords("select?*?from?news"));?? $result=$db->getFields("select?*?from?news");??
  42. for($i=0;$i<count($result);$i++){?? ????echo?$result[$i]."<br?/>";??
  43. }??$db->Close();??
  44. ?>??

热点排行