|
|
|
How to Insert JSON Data into MySQL using PHP? |
|
|
|
|
|
|
|
How to Insert JSON Data into MySQL using PHP?
Code (PHP)
<?php
// database connection
require_once('DataBaseConnection.php');
$jsondata='{
"Day 1" :
{
"ei_end_time" : "12:23 PM",
"ei_itinerary_name" : "dsa",
"ei_program_date" : "Oct 09, 2014",
"ei_day_key" : "Day 1",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "12:23 PM",
"ei_itinerary_location" : "asd"
},
{
"ei_end_time" : "12:02 PM",
"ei_itinerary_name" : "asd",
"ei_program_date" : "1",
"ei_day_key" : "Program 1",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "2:00 PM",
"ei_itinerary_location" : "asd"
}
,
"Day 2" :
{
"ei_end_time" : "12:01 PM",
"ei_itinerary_name" : "asd",
"ei_program_date" : "Mar 8, 2015",
"ei_day_key" : "Day 2",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "2:00 PM",
"ei_itinerary_location" : "asd"
}
}';
$json_array = json_decode($jsondata);
echo $json_array['Day 1']['ei_end_time'];
mysql_close($objConnect);
?>
I was use this method but not working:
http://www.kodingmadesimple.com/2014/12/how-to-insert-json-data-into-mysql-php.html
Tag : PHP, MySQL
|
ประวัติการแก้ไข 2015-01-08 15:28:07
|
|
|
|
|
Date :
2015-01-08 15:18:18 |
By :
vishwa |
View :
981 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for Example
<?php
// Encode the data.
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
var_dump(json_decode($json, true, $depth));
echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
?>
|
|
|
|
|
Date :
2015-01-09 09:34:53 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Your format of $jsondata is object that is not array.
if you want use it by array then you must change format to array
change {} to []
Code (PHP)
$jsondata='
[
"Day 1" :
[
"ei_end_time" : "12:23 PM",
"ei_itinerary_name" : "dsa",
"ei_program_date" : "Oct 09, 2014",
"ei_day_key" : "Day 1",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "12:23 PM",
"ei_itinerary_location" : "asd"
],
// this array has not key name
[
"ei_end_time" : "12:02 PM",
"ei_itinerary_name" : "asd",
"ei_program_date" : "1",
"ei_day_key" : "Program 1",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "2:00 PM",
"ei_itinerary_location" : "asd"
],
"Day 2" :
[
"ei_end_time" : "12:01 PM",
"ei_itinerary_name" : "asd",
"ei_program_date" : "Mar 8, 2015",
"ei_day_key" : "Day 2",
"ei_itinerary_descreiption" : "",
"ei_start_time" : "2:00 PM",
"ei_itinerary_location" : "asd"
]
]';
|
|
|
|
|
Date :
2015-01-09 10:23:04 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thank you all.
Now working fine
|
|
|
|
|
Date :
2015-01-09 11:09:03 |
By :
vishwa |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|