我试图获取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插入数据库时,我没有得到数组值。
答案 0 :(得分:2)
$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) ."', ...)")
但是,这样几乎不可能搜索/过滤标签,因此您应该将它们存储在不同的表格中,以便将单个标签与单个电影相关联。
答案 1 :(得分:0)
不确定这是否是问题,但在这一行:
$video_tags[0] = $video_obj->tags-tag[0];
您似乎要么在属性名称中使用短划线,要么忘记了标记变量上的美元符号。