我们在做缓存文件时经常会要把php代码或数组转换成字符串保存到数据库中,下面我来介绍两种把数组保存到数据库的方法。
方法一:用serialize写入,再用unserialize输出
serialize()就是将PHP中的变量如对象(object),数组(array)等等的值序列化为字符串后存储起来.序列化的字符串我们可以 存储在其他地方如数据库、Session、Cookie等,序列化的操作并不会丢失这些值的类型和结构。这样这些变量的数据就可以在PHP页面、甚至是不 同PHP程序间传递了。
而unserialize()就是把序列化的字符串转换回PHP的值。返回的是转换之后的值,可为 integer、float、string、array 或 object如果传递的字符串不可解序列化,则返回 FALSE,代码如下:
- class db {
- private $host;
- private $user;
- private $pwd;
- private $dbname;
- private $Mysqli;
- function __construct($host, $user, $pwd, $dbname) {
- $this->host = $host;
- $this->user = $user;
- $this->pwd = $pwd;
- $this->dbname = $dbname;
- $this->db();
- }
- function db() {
- $this->mysqli = new mysqli ( $this->host, $this->user, $this->pwd, $this->dbname );
- }
- function select() {
- $this->mysqli->query("SET CHARSET GBK");
- $sql = "SELECT id,cname FROM hdw_channel";
- $result = $this->mysqli
- ->query ( $sql );
- $rows = array ();
- while ( $row = $result->fetch_assoc () ) {
- $rows [] = $row;
- }
- ECHO "<PRE>";
- print_r ( $rows );
- }
- function __wakeup(){ //反序列化,
- $this->db();
- }
- }
- $chanel = new db("localhost",'root','','hdcms');
- //$chanel->select();
- session_start();
- $_SESSION['channel_obj'] = serialize($chanel); //将对象序列化,保存的是对象的属性,没有方法,所以要用__wakeup()
- class ren{
- private $name;
- private $age;
- function __construct($name,$age){
- $this->name =$name;
- $this->age = $age;
- }
- function show(){
- echo "姓名是:{$this->name} 年龄是:{$this->age}";
- }
- function __sleep(){
- return array_keys(get_object_vars($this)); //或得数组里边的键名,序列化某些变量
- }
- }
- $zao = new ren("赵六",44);
- echo serialize($zao); //序列化(指定哪个变量序列化)
- ====================================
- session_start();
- include '59.php';
- $channel_obj=unserialize($_SESSION['channel_obj']); //反序列化类对象
- $channel_obj->select(); //有了__wakeup方法才可以起作用
方法二:用json_encode写入,再用json_decode输出
json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来,代码如下:
- <?php
- /**************************************************************
- *
- * 使用特定function对数组中所有元素做处理
- * @param string &$array 要处理的字符串
- * @param string $function 要执行的函数
- * @return boolean $apply_to_keys_also 是否也应用到key上
- * @access public
- *
- *************************************************************/
- function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
- {
- static $recursive_counter = 0;
- if (++$recursive_counter > 1000) {
- die('possible deep recursion attack');
- }
- foreach ($array as $key => $value) {
- if (is_array($value)) {
- arrayRecursive($array[$key], $function, $apply_to_keys_also);
- } else {
- $array[$key] = $function($value);
- }
- if ($apply_to_keys_also && is_string($key)) {
- $new_key = $function($key);
- if ($new_key != $key) {
- $array[$new_key] = $array[$key];
- unset($array[$key]);
- }
- }
- }
- $recursive_counter–;
- }
- /**************************************************************
- *
- * 将数组转换为JSON字符串(兼容中文)
- * @param array $array 要转换的数组
- * @return string 转换得到的json字符串
- * @access public
- *
- *************************************************************/
- function JSON($array) {
- arrayRecursive($array, 'urlencode', true);
- $json = json_encode($array);
- return urldecode($json);
- }
- $array = array
- (
- 'Name'=>'希亚',
- 'Age'=>20
- );
- echo JSON($array);
- ?>
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php把数组保存数据库程序代码
波比源码 » php把数组保存数据库程序代码
levofloxacin 500mg sale buy generic levofloxacin
dutasteride without prescription oral zofran 4mg ondansetron 8mg canada
aldactone 25mg pills cheap valacyclovir order fluconazole online
ampicillin 500mg over the counter ampicillin 250mg pill buy erythromycin 250mg online
buy fildena pill order robaxin 500mg sale oral methocarbamol
buy sildenafil 50mg without prescription purchase estradiol generic cost estrace 2mg
lamictal 50mg tablet cheap tretinoin cream order tretinoin generic
isotretinoin 40mg cheap isotretinoin 10mg uk azithromycin pills
arimidex 1 mg drug clarithromycin 250mg price sildenafil order online
tadalafil kaufen generika original viagra 100mg rezeptfrei sicher kaufen viagra 100mg ohne rezept
purchase accutane for sale stromectol 3mg tablet stromectol order online
order catapres generic buy spiriva 9mcg pills purchase spiriva for sale
terazosin 1mg uk hytrin 5mg without prescription brand sulfasalazine 500mg
alendronate 35mg drug buy generic pepcid 40mg buy famotidine 20mg
benicar 10mg pill order acetazolamide 250 mg generic order diamox 250mg without prescription
order tacrolimus 1mg without prescription purchase trandate generic ursodiol oral
buy isosorbide 40mg brand imuran brand micardis 20mg
order sertraline 100mg generic Rx generic viagra viagra online order
salbutamol 100 mcg uk imuran ca order sildenafil 100mg pill
buy cialis online Buy cheap cialis brand viagra pills
buy generic cialis 20mg buy amantadine pills order amantadine 100 mg
brand naltrexone buy letrozole 2.5 mg pill order abilify 30mg generic
dapsone 100mg generic purchase fexofenadine pills aceon sale
cheap medroxyprogesterone 10mg purchase praziquantel generic buy periactin generic
modafinil 100mg generic provigil online buy stromectol 0.1
fluvoxamine 50mg ca buy glucotrol for sale buy glipizide 10mg pill
order nootropil 800mg without prescription sildenafil australia free shipping viagra
order azithromycin generic buy gabapentin 100mg without prescription cheap neurontin generic
lasix 40mg without prescription order furosemide 40mg pill order plaquenil 400mg pills
purchase chloroquine online order cenforce online order baricitinib for sale
glucophage generic atorvastatin 20mg pill cialis prices
buy olanzapine 10mg sale valsartan online buy valsartan 80mg cost
buy generic norvasc 10mg sildenafil mail order us generic tadalafil
clozaril for sale online order clozaril 50mg generic oral decadron 0,5 mg
viagra pill lisinopril 10mg drug lisinopril 2.5mg sale
buy prilosec 20mg pills order omeprazole roulette online real money
buy linezolid 600 mg without prescription doubleu casino online casino playing poker online
metoprolol 50mg canada metoprolol 100mg usa order vardenafil pills
paper assistance buy a dissertation slot online
levitra 20mg generic buy methylprednisolone 16 mg methylprednisolone online
cheap tadalafil generic overnight cialis delivery cheap sildenafil pills
buy dapoxetine pill cytotec cheap order synthroid 100mcg without prescription
order tadalafil 40mg online cheap sildenafil 200mg price sildenafil 100mg sale
buy tadalafil 40mg online cheap buy inderal order plavix 150mg online
pfizer cialis clopidogrel 150mg pill order clopidogrel 150mg pill
order methotrexate 5mg online cheap metoclopramide 10mg drug metoclopramide for sale online
purchase losartan sale losartan cheap topiramate 100mg pill
purchase baclofen order ozobax for sale order toradol 10mg for sale
colchicine 0.5mg cost colchicine 0.5mg over the counter casino slot free
casino games win real money online blackjack casino free casino games no registration no download
order diflucan 200mg for sale fluconazole generic buy sildenafil 100mg online cheap
keflex cheap clindamycin pills order erythromycin generic
cialis pills 40mg us pharmacy viagra viagra medication
cefuroxime 250mg us order generic robaxin 500mg buy methocarbamol 500mg online
spins real money online roulette game real money cialis 10mg price
order sildalis sildenafil pills 50mg brand lamotrigine
prednisone 5mg price buy deltasone online brand amoxicillin 500mg
oral azithromycin 250mg buy neurontin for sale buy neurontin online cheap
buy sildenafil 50mg generic oral budesonide rhinocort canada
order retin gel online avana 200mg drug avanafil over the counter
oral tadacip 10mg indocin for sale buy indocin 75mg generic
terbinafine medication terbinafine price trimox ca
tiotropium bromide 9mcg drug buy minocycline generic terazosin brand
order montelukast 10mg online cheap sildenafil ca order sildenafil 100mg pill
order tadalafil 10mg for sale cost tadalafil 40mg cost tadalafil 40mg
free spins no deposit uk free roulette games online gambling games
free slot play casino card games slots casino
buy doxycycline 200mg online cheap doxycycline uk purchase cleocin for sale
buy mesalamine 400mg online cheap azelastine 10 ml ca order avapro 150mg for sale
order temovate online cheap buspar 5mg cost amiodarone 200mg for sale
order carvedilol 25mg online cheap order ditropan generic order elavil 50mg generic
buy dapoxetine 90mg pill domperidone generic order motilium for sale
buy indomethacin 75mg generic cenforce online buy order generic cenforce 50mg
tadalafil 10mg over the counter tadacip oral trimox pills
oral requip requip pills purchase labetalol pills
buy esomeprazole capsules clarithromycin 500mg uk generic lasix 100mg
order tadalafil 40mg online cheap real levitra best otc ed pills
oral modafinil 100mg buy promethazine 25mg sale oral phenergan
propranolol brand coreg usa buy carvedilol online
uroxatral online order uroxatral online purchase diltiazem generic
promethazine 25mg without prescription modafinil pills tadalafil 40mg pills
buy generic tadalafil buy cialis overnight delivery medicine erectile dysfunction
ivermectin cost australia deltasone 10mg brand accutane without prescription
buy accutane 10mg sale order amoxicillin 500mg online cheap buy azithromycin medication
purchase imitrex pill buy dutasteride without prescription buy avodart without a prescription
clopidogrel 150mg usa buy luvox no prescription order nizoral for sale
flomax 0.2mg canada aldactone 100mg usa aldactone tablet
buy simvastatin generic buy valacyclovir tablets cheap viagra for sale
order generic cialis 40mg Viagra pfizer viagra order
cefadroxil sale order finasteride 5mg pills proscar order
order fluconazole generic fluconazole tablet ciprofloxacin 1000mg drug
purchase estrace without prescription lamictal price buy minipress sale
buy flagyl no prescription keflex pills buy generic keflex online
cleocin sale erythromycin for sale order fildena generic
avanafil online order avanafil 100mg price purchase voltaren
buy generic careprost for sale order trazodone 50mg online cheap cheap desyrel
order suhagra 50mg generic sildenafil next day sildenafil uk
azithromycin 250mg sale buy generic omnacortil 20mg how to get neurontin without a prescription
order lasix 100mg monodox canada albuterol pills
ramipril online order altace 10mg sale arcoxia 60mg pill
asacol price buy irbesartan 150mg without prescription avapro 300mg us
diamox pills order diamox generic order azathioprine 50mg sale
buy digoxin generic digoxin pill buy molnunat sale
order naprosyn without prescription naproxen online buy prevacid generic
olumiant ca order atorvastatin pills lipitor online buy
order montelukast sale symmetrel tablet dapsone online
where can i buy nifedipine adalat pills allegra 180mg oral
buy amlodipine 10mg generic purchase prinivil without prescription omeprazole canada
order generic dapoxetine order dapoxetine sale orlistat 120mg ca
order diltiazem 180mg buy zyloprim 100mg pill how to buy zyloprim
crestor where to buy rosuvastatin pills order domperidone 10mg pills
buy tetracycline 250mg online cyclobenzaprine order baclofen 25mg pills
buy cheap generic toradol buy propranolol cheap purchase inderal for sale
brand erythromycin 500mg cost fildena order nolvadex for sale
plavix 150mg oral warfarin 2mg pills warfarin brand
buy rhinocort tablets purchase budesonide online cheap buy generic bimatoprost
brand topiramate 100mg purchase topamax generic levaquin 500mg uk
methocarbamol 500mg oral buy sildenafil 100mg oral suhagra
avodart 0.5mg canada purchase meloxicam pill buy mobic 7.5mg pills
sildenafil 100mg over the counter sildenafil women order estrace 2mg without prescription
celecoxib oral celebrex online ondansetron 8mg uk
order spironolactone 25mg pill zocor 10mg tablet valtrex 1000mg over the counter
oral tadalafil 40mg purchase viagra generic sildenafil 100mg us
buy generic lamisil order cefixime 200mg pill buy amoxicillin 250mg generic
sulfasalazine 500 mg drug order olmesartan 10mg verapamil oral
cheap divalproex order acetazolamide pills buy imdur 40mg
imuran pill order generic lanoxin micardis canada
pyridium where to buy buy symmetrel 100 mg online cheap buy amantadine 100mg generic
oral fexofenadine 180mg allegra 180mg without prescription cheap amaryl
arcoxia 120mg usa order etoricoxib 120mg online azelastine sale
order avapro online buy avapro cheap buspirone 10mg oral
order albendazole 400mg for sale order albenza 400mg pills buy generic medroxyprogesterone 5mg
praziquantel for sale praziquantel 600mg canada order periactin 4mg generic
luvox 100mg pills buy nizoral pills order duloxetine 40mg generic
buy glipizide 5mg pill buy betamethasone 20gm online cheap buy betnovate cheap
buy paracetamol 500 mg pills order generic pepcid order pepcid 40mg pill
order tindamax pill oral olanzapine 10mg purchase nebivolol online cheap
buy diovan 80mg generic combivent drug order combivent generic
order decadron 0,5 mg sale oral dexamethasone 0,5 mg order nateglinide 120mg sale
brand captopril 25 mg buy capoten sale order tegretol 200mg generic
buy ciplox 500mg pills duricef 250mg price buy duricef 500mg for sale
order generic seroquel 100mg escitalopram for sale online cheap lexapro 10mg
where to buy epivir without a prescription order epivir online quinapril brand
sarafem 20mg tablet buy sarafem without prescription cheap femara 2.5 mg
cost frumil buy adapen gel cheap zovirax
buy zebeta paypal pill lozol buy terramycin 250 mg pills