php把数组保存数据库程序代码

我们在做缓存文件时经常会要把php代码或数组转换成字符串保存到数据库中,下面我来介绍两种把数组保存到数据库的方法。

方法一:用serialize写入,再用unserialize输出

serialize()就是将PHP中的变量如对象(object),数组(array)等等的值序列化为字符串后存储起来.序列化的字符串我们可以 存储在其他地方如数据库、Session、Cookie等,序列化的操作并不会丢失这些值的类型和结构。这样这些变量的数据就可以在PHP页面、甚至是不 同PHP程序间传递了。

而unserialize()就是把序列化的字符串转换回PHP的值。返回的是转换之后的值,可为 integer、float、string、array 或 object如果传递的字符串不可解序列化,则返回 FALSE,代码如下:

  1. class db { 
  2.  private $host
  3.  private $user
  4.  private $pwd
  5.  private $dbname
  6.  private $Mysqli
  7.  function __construct($host$user$pwd$dbname) { 
  8.   $this->host = $host
  9.   $this->user = $user
  10.   $this->pwd = $pwd
  11.   $this->dbname = $dbname
  12.   $this->db(); 
  13.  } 
  14.  function db() { 
  15.   $this->mysqli = new mysqli ( $this->host, $this->user, $this->pwd, $this->dbname ); 
  16.  } 
  17.  function select() { 
  18.   $this->mysqli->query("SET CHARSET GBK"); 
  19.   $sql = "SELECT id,cname FROM hdw_channel"
  20.   $result = $this->mysqli 
  21.    ->query ( $sql ); 
  22.   $rows = array (); 
  23.   while ( $row = $result->fetch_assoc () ) { 
  24.    $rows [] = $row
  25.   } 
  26.   ECHO "<PRE>"
  27.   print_r ( $rows ); 
  28.  } 
  29.  function __wakeup(){   //反序列化, 
  30.   $this->db(); 
  31.  } 
  32. $chanel = new db("localhost",'root','','hdcms'); 
  33. //$chanel->select(); 
  34. session_start(); 
  35. $_SESSION['channel_obj'] = serialize($chanel);   //将对象序列化,保存的是对象的属性,没有方法,所以要用__wakeup() 
  36.  
  37. class ren{ 
  38.  private $name
  39.  private $age
  40.  function __construct($name,$age){ 
  41.   $this->name =$name
  42.   $this->age = $age
  43.  } 
  44.  function show(){ 
  45.   echo "姓名是:{$this->name}  年龄是:{$this->age}"
  46.  } 
  47.  function __sleep(){ 
  48.   return array_keys(get_object_vars($this));  //或得数组里边的键名,序列化某些变量 
  49.  } 
  50. $zao = new ren("赵六",44); 
  51. echo serialize($zao);       //序列化(指定哪个变量序列化) 
  52. ==================================== 
  53. session_start(); 
  54. include '59.php'
  55. $channel_obj=unserialize($_SESSION['channel_obj']);  //反序列化类对象 
  56. $channel_obj->select();  //有了__wakeup方法才可以起作用 

方法二:用json_encode写入,再用json_decode输出

json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来,代码如下:

  1. <?php 
  2. /************************************************************** 
  3.  * 
  4.  * 使用特定function对数组中所有元素做处理 
  5.  * @param string &$array  要处理的字符串 
  6.  * @param string $function 要执行的函数 
  7.  * @return boolean $apply_to_keys_also  是否也应用到key上 
  8.  * @access public 
  9.  * 
  10.  *************************************************************/ 
  11. function arrayRecursive(&$array$function$apply_to_keys_also = false) 
  12.     static $recursive_counter = 0; 
  13.     if (++$recursive_counter > 1000) { 
  14.         die('possible deep recursion attack'); 
  15.     } 
  16.     foreach ($array as $key => $value) { 
  17.         if (is_array($value)) { 
  18.             arrayRecursive($array[$key], $function$apply_to_keys_also); 
  19.         } else { 
  20.             $array[$key] = $function($value); 
  21.         } 
  22.  
  23.         if ($apply_to_keys_also && is_string($key)) { 
  24.             $new_key = $function($key); 
  25.             if ($new_key != $key) { 
  26.                 $array[$new_key] = $array[$key]; 
  27.                 unset($array[$key]); 
  28.             } 
  29.         } 
  30.     } 
  31.     $recursive_counter–; 
  32.  
  33. /************************************************************** 
  34.  * 
  35.  * 将数组转换为JSON字符串(兼容中文) 
  36.  * @param array $array  要转换的数组 
  37.  * @return string  转换得到的json字符串 
  38.  * @access public 
  39.  * 
  40.  *************************************************************/ 
  41. function JSON($array) { 
  42.  arrayRecursive($array'urlencode', true); 
  43.  $json = json_encode($array); 
  44.  return urldecode($json); 
  45. $array = array 
  46.        ( 
  47.           'Name'=>'希亚'
  48.           'Age'=>20 
  49.        ); 
  50.  
  51. echo JSON($array); 
  52. ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php把数组保存数据库程序代码

146 评论

  1. dutasteride without prescription oral zofran 4mg ondansetron 8mg canada

  2. buy isosorbide 40mg brand imuran brand micardis 20mg

  3. order sertraline 100mg generic Rx generic viagra viagra online order

  4. salbutamol 100 mcg uk imuran ca order sildenafil 100mg pill

  5. order nootropil 800mg without prescription sildenafil australia free shipping viagra

  6. buy prilosec 20mg pills order omeprazole roulette online real money

  7. buy dapoxetine pill cytotec cheap order synthroid 100mcg without prescription

  8. buy tadalafil 40mg online cheap buy inderal order plavix 150mg online

  9. purchase losartan sale losartan cheap topiramate 100mg pill

  10. order diflucan 200mg for sale fluconazole generic buy sildenafil 100mg online cheap

  11. buy sildenafil 50mg generic oral budesonide rhinocort canada

  12. order retin gel online avana 200mg drug avanafil over the counter

  13. order montelukast 10mg online cheap sildenafil ca order sildenafil 100mg pill

  14. buy doxycycline 200mg online cheap doxycycline uk purchase cleocin for sale

  15. buy mesalamine 400mg online cheap azelastine 10 ml ca order avapro 150mg for sale

  16. order temovate online cheap buspar 5mg cost amiodarone 200mg for sale

  17. tadalafil 10mg over the counter tadacip oral trimox pills

  18. oral requip requip pills purchase labetalol pills

  19. order tadalafil 40mg online cheap real levitra best otc ed pills

  20. propranolol brand coreg usa buy carvedilol online

  21. promethazine 25mg without prescription modafinil pills tadalafil 40mg pills

  22. order generic cialis 40mg Viagra pfizer viagra order

  23. purchase estrace without prescription lamictal price buy minipress sale

  24. buy flagyl no prescription keflex pills buy generic keflex online

  25. buy digoxin generic digoxin pill buy molnunat sale

  26. where can i buy nifedipine adalat pills allegra 180mg oral

  27. brand erythromycin 500mg cost fildena order nolvadex for sale

  28. sildenafil 100mg over the counter sildenafil women order estrace 2mg without prescription

  29. order spironolactone 25mg pill zocor 10mg tablet valtrex 1000mg over the counter

  30. buy diovan 80mg generic combivent drug order combivent generic

  31. brand captopril 25 mg buy capoten sale order tegretol 200mg generic

  32. buy zebeta paypal pill lozol buy terramycin 250 mg pills

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡