Mysql查询多个数组值

时间:2015-04-21 00:35:21

标签: php mysql arrays video

我有这个问题:

$relatedtags = $video->tags;
$relatedtagsagain = explode(",", $relatedtags);
$query_parts = array();
foreach ($relatedtagsagain as $item) {
$query_parts[] = "'%".mysql_real_escape_string($item)."%'";}
$string = implode(",", $query_parts);

$result = $cachedb->get_results("SELECT ".DB_PREFIX."videos.title,".DB_PREFIX."videos.id as vid,".DB_PREFIX."videos.thumb, ".DB_PREFIX."videos.views,".DB_PREFIX."videos.duration,".DB_PREFIX."users.name, ".DB_PREFIX."users.id as owner FROM ".DB_PREFIX."videos LEFT JOIN ".DB_PREFIX."users ON ".DB_PREFIX."videos.user_id = ".DB_PREFIX."users.id where ".DB_PREFIX."videos.tags LIKE {$query_parts[0]} OR ".DB_PREFIX."videos.tags LIKE {$query_parts[1]} limit 0,".get_option('related-nr')." ");

如何获得更多$query_parts[] like $query_parts[2]$query_parts[3]或来自所有阵列的结果?使用LIKE $query_parts[]无法正常工作。

1 个答案:

答案 0 :(得分:0)

让我们回到编程的基础,这可以通过多种方式解决,比如使用array_mapping(请参阅scrowler的注释),循环等等:

使用foreach循环的示例:

foreach ($relatedtagsagain as $item) {
  $query_parts[] = " videos.tags LIKE " . "'%". mysql_real_escape_string($item) . "%' ";
}
$parsed_query_parts = implode('OR', $query_parts);

$result = $cachedb->get_results("SELECT ".DB_PREFIX."videos.title,".DB_PREFIX."videos.id as vid,".DB_PREFIX."videos.thumb, ".DB_PREFIX."videos.views,".DB_PREFIX."videos.duration,".DB_PREFIX."users.name, ".DB_PREFIX."users.id as owner FROM ".DB_PREFIX."videos LEFT JOIN ".DB_PREFIX."users ON ".DB_PREFIX."videos.user_id = ".DB_PREFIX."users.id WHERE" . $parsed_query_parts . " limit 0,".get_option('related-nr')." ");