如何使用PHP从数据库中检索存储的post数组

时间:2016-09-26 08:25:18

标签: php mysql arrays

我将表格后期数据以数组格式存储在MySQL数据库中,但是当我获取值时,它不是数组,它就变成了字符串。

有我的提取值:

Array ( [First_Name] => Rahul [Last_Name] => Singh [Street] => 210 Adhichini [City] => New Delhi [California_Automobile_Insurance_Quote] => California Automobile [ZIP_/_Postal_Code] => 110092 [E-Mail_Address] => rahulsingh4ut@gmail.com [Primary_Phone_Number] => 8470016156 [Alternate_Phone_Number] => 7503963073 [Date_of_Birth] => 25/05/1990 [Marital_Status] => Single [License_(State,_Number)] => DLI123 [Year] => 2012 [Make] => TVS [Model] => Apache 160 [VIN] => TVS123 [Cylinders] => CY000 [Do_you_rent_or_own_your_home?] => Rent [Do_you_currently_have_insurance?] => No [Current_Insurance_Provider] => No I. Prov [last_insurance] => No [Comprehensive_Deductible] => No [Collision_Deductible] => No [Bodily_Injury_Liability] => Bodily [Property_Damage_Liability] => Property Damage [Uninsured_Motorist_Bodily_Injury] => Uninsured [Uninsured_Motorist_Property_Damage] => Motorist Property Da [Underinsured_Motorist_-_Bodily_Injury_Limits] => Motorist - Bodily [Underinsured_Motorist_-_Property_Damage_Limits] => 05 [Medical_Pay_/_PIP] => 2016 [Towing] => Delhi [What_percentage_of_your_vehicles_total_use_time_is_driven_by_you?] => 45 [How_many_miles] => 15000 [accidents_or_minor_violations] => 5Yr ) 

2 个答案:

答案 0 :(得分:1)

我认为您使用序列化函数将数据发布到数据库中,并使用unserialize函数检索数据。这是最好的

$serialized_data = serialize(array('Math', 'Language', 'Science'));  
echo  $serialized_data . '<br>';  

答案 1 :(得分:1)

您无法在MySQL中存储数组,但您可以选择将数组转换为JSON StringSerialized String。在MySQL数据库中可视化存储Array是很困难的......你可以这样做(例如):

<?php
     // THIS IS JUST AN EXAMPLE, IN REALITY, YOU HAVE TO CLEAN UP
     // ALL THE DATA COMING FROM YOUR $_POST GLOBAL TO AVOID RISKS OF SQL INJECTION.
     $postDataForDB = json_encode($_POST);
     // NOW YOU CAN SAVE THE RESULTING JSON ENCODED DATA TO DATABASE...

可替换地;您可以使用数据的序列化版本,如下所示:

<?php
     // AGAIN; THIS IS JUST AN EXAMPLE, IN REALITY, YOU HAVE TO CLEAN UP
     // ALL THE DATA COMING FROM YOUR $_POST GLOBAL TO AVOID RISKS OF SQL INJECTION.
     $postDataForDB = serialize($_POST);
     // NOW YOU CAN SAVE THE RESULTING SERIALIZED DATA TO DATABASE...