我对这种PHP /查询有点新,所以请给我一些懒散的哈哈。
我有以下内容。
称为$ event的帖子类型和管理$ events的插件。长话短说,事件的位置存储在自定义表中。每个事件都有一个后元“_location_id”,它对应于wp_em_locations表中的行ID。
我可以使用:
获取_location_idget_post_meta($eventID, '_location_id', true);
但是,我不知道如何从与自定义表中的行ID对应的不同列中获取值。
我的目的是显示一个事件列表,旁边有位置名称。
我现在正在尝试下面的代码,编辑唯一的id列名称和列名称我希望从中获取数据并且我什么都不返回。
<?php
global $wpdb;
$location_id=get_post_meta($eventID, '_location_id', true);
echo $location_id;
//the above returns the correct row ID
$locations=$wpdb->get_results('select {location_name} from `' . $wpdb->prefix . 'em_locations` where `location_id`=\'' . $location_id . '\'', ARRAY_A);
if (!is_null($locations) && count($locations) > 0){
foreach ($locations as $location){
echo $location['location_name'];
}
} else {
//no locations
} ?>
@ Flyer的例子对我有用,所以我想也许我做错了所以检查了一下编码,然后跟着以下内容。
global $wpdb;
$location_id=get_post_meta($eventID, '_location_id', true);
$myrows = $wpdb->get_results( 'SELECT location_id, location_name FROM wp_em_locations WHERE location_id = '. $location_id );
var_dump($myrows);
/* gives the following
array(1) { [0]=> object(stdClass)#215 (2) { ["location_id"]=> string(1) "5" ["location_name"]=> string(9) "Liverpool" } }*/
echo $myrows["location_name"];
//shows nothing
答案 0 :(得分:2)
global $wpdb;
$location_id=get_post_meta($eventID, '_location_id', true);
$locations=$wpdb->get_results('select `location_id`,`location_name`,`location_town`, `location_city` from `' . $wpdb->prefix . 'em_locations` where `location_id`=\'' . $location_id . '\'', ARRAY_A);
if (!is_null($locations) && count($locations) > 0)
{
foreach ($locations as $location)
{
//do whatever you need with results, accessing them like $location['ID'] for example
}
}
else
{
//no locations
}