Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用post meta从wordpress中的自定义表中获取值_Php_Mysql_Wordpress - Fatal编程技术网

Php 使用post meta从wordpress中的自定义表中获取值

Php 使用post meta从wordpress中的自定义表中获取值,php,mysql,wordpress,Php,Mysql,Wordpress,我对这种PHP/查询有点陌生,所以请帮我放松一下 我有以下几点 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_dum

我对这种PHP/查询有点陌生,所以请帮我放松一下

我有以下几点

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
一个名为$event的Post类型和一个管理$events的插件。长话短说事件的位置存储在自定义表中。每个事件都有一个post meta“\u location\u id”,它对应于wp\u em\u locations表中的行id

我可以通过以下方式获取_location_id:

get_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
    } ?>    

到目前为止,最好的答案是试试看。我在上面的编辑中添加了您的示例,但是它不起作用,您觉得怎么样?@AlexKnopp您不需要
{}
,这表明
您需要的列列表
只是一个占位符。我对其进行了编辑以获取ID和位置名称(假设
location\u name
是该列在DB中的实际名称),很抱歉造成混淆。表中的主要(第一)列是“location\u id”,我需要的列是“location\u name、location\u town和location\u city”。另外,我已经在循环中了,我需要foreach吗?@AlexKnopp更新以反映所需的列。是的,这个foreach遍历mysql查询返回的位置(行)
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
}