我正在尝试获取 json 文件的值并将其插入到 mysql 数据库中。
我从 json 文件中获取所有对象值,但是当我尝试获取数组内数组的值时,我只需插入到每一行中:“array”
我使用以下代码(为此目的而修复):
<?php
set_time_limit(0);
function ApiCall($http_server, $params = array())
{
$query_string = '?';
if(is_array($params) && count($params)){
foreach($params as $k=>$v){
$query_string .= $k.'='.$v.'&';
}
$query_string = rtrim($query_string,'&');
}
return file_get_contents($http_server.$query_string);
}
function getVideos($page = 0, $category = false)
{
$http = 'http://api.domain.com/';
$call = 'reference';
$params = array(
'output' => 'json',
'data' => $call,
'page' => $page
);
if($category){
$params['category'] = $category;
}
$response = ApiCall($http , $params);
if ($response) {
$json = json_decode($response);
if(isset($json->code) && isset($json->message)){
throw new Exception($json->message, $json->code);
}
return $json;
}
return false;
}
$page = 1;
$pages = 2;
while(true) {
try{
if($page > $pages)
break;
$videoResponse = getVideos($page);
$videos = $videoResponse->videos;
$con=mysqli_connect("","","","");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
foreach($videos as $video) {
$video_obj = $video->video;
$video_id = $video_obj->video_id;
$video_title = $video_obj->title;
$video_tags = array();
$video_tags[0] = $video_obj->tags-tag[0];
$video_tags[1] = $video_obj->tags-tag[1];
$video_tags[2] = $video_obj->tags-tag[2];
$video_tags[3] = $video_obj->tags-tag[3];
$video_tags[4] = $video_obj->tags-tag[4];
$video_tags[5] = $video_obj->tags-tag[5];
$video_tags[6] = $video_obj->tags-tag[6];
$video_tags[7] = $video_obj->tags-tag[7];
$video_tags[8] = $video_obj->tags-tag[8];
$video_tags[9] = $video_obj->tags-tag[9];
$video_tags[10] = $video_obj->tags-tag[10];
$video_thumbnail = $video_obj->thumb;
$video_embed = $video_obj->embed_url;
$video_duration = $video_obj->duration;
$video_url = $video_obj->url;
$friendlyUrl = preg_replace('/^-+|-+$/', '', strtolower(preg_replace('/[^a-zA-Z0-9]+/', '-', $video_title)));
$result = mysqli_query($con,"INSERT INTO movies (movie_id, title, tags, stars, thumbnail, thumbnails, embed, length, location, friendlyUrl)
VALUES ('". $video_id ."','". $video_title ."', '". $video_tags ."', '". $video_stars ."', '". $video_thumbnail ."', '". $video_thumbnails ."',
'". $video_embed ."', '". $video_duration ."', '". $video_url ."', '". $friendlyUrl ."')")
or die(mysqli_error());
}
$pages = $videoResponse->count % 20 == 0 ? $videoResponse->count / 20 : floor($videoResponse->count) / 20;
$page++;
unset($videoResponse);
unset($videos);
sleep(2);
} catch(Exception $e){
// Something is wrong with the response. You should handle that exception.
}
}
?>
将 $video_tags 插入数据库时,我没有获得数组值。
最佳答案
$video_tags
是一个数组:
$video_tags = array();
$video_tags[0] = $video_obj->tags-tag[0];
// ^ is that just a typo?
// $video_tags[0] = $video_obj->tags->tag[0]; ???
// etc.
因此您无法将其插入此处的查询中:
$result = mysqli_query($con,"INSERT INTO movies (...)
VALUES (... , '". $video_tags ."', ...)")
^^^^^^^^^^^ here
理想情况下,您应该有一个不同的表来存储标签,但如果您确实必须将它们存储在一个字段中,则需要 serialize它们或将它们存储为例如 json 字符串:
$result = mysqli_query($con,"INSERT INTO movies (...)
VALUES (... , '". serialize($video_tags) ."', ...)")
但是,这将使搜索/过滤标签几乎变得不可能,因此您确实应该将它们存储在不同的表中,在该表中将各个标签与各个电影链接起来。
关于php - 使用 PHP 进行 JSON 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29541232/