Database 使用wpdb从wordpress数据库访问JSON结果

Database 使用wpdb从wordpress数据库访问JSON结果,database,wordpress,Database,Wordpress,我已经将Google API的JSON结果添加到wp_选项表中。 在这一点上,我只需要回应特定的元素。即坐标。 我的选项名称是py菜单项。 选项_值元素之一是coords。这就是表示JSON结果的内容。 下面的代码生成此响应:array(o){} 守则: global $wpdb; $myrows = $wpdb->get_results("SELECT coords FROM $wpdb->wp_options"); $lat = $myrows->results[0]-&g

我已经将Google API的JSON结果添加到wp_选项表中。 在这一点上,我只需要回应特定的元素。即坐标。 我的选项名称是py菜单项。 选项_值元素之一是coords。这就是表示JSON结果的内容。 下面的代码生成此响应:
array(o){}

守则:

global $wpdb;
$myrows = $wpdb->get_results("SELECT coords FROM $wpdb->wp_options");
$lat = $myrows->results[0]->geometry->location->lat;    //latitude
$long = $myrows->results[0]->geometry->location->lng;   //longitude 
echo $lat;
echo $long;
var_dump($myrows);

我用这个法典页面作为指导:

使用get\u选项,它将简化很多事情

function test(){
    //Note no need to reference $wpdb global. 
    //My code here is more verbose than needed, but it explains each step.
    $jsonString = get_option('py_menu_item');//get the val from the options table
    $jsonArray = json_decode($jsonString);   //decode the JSON serialized string
    $coords = $jsonArray['coords'];          //get reference to the attribute
    var_dump($coords);                       //do what you want with it
}

对不起,这似乎不起作用。”coords'已经被解码了,如果这有区别的话。因此,var_从get_option()转储数据,查看'coords'在数组中的位置,并相应地调用它。顺便说一句,$coords=$jsonArray('coords');这是错误的。它应该是$coords=$jsonArray['coords'];