打印表

时间:2016-05-31 04:04:57

标签: mysql wordpress wpdb

问题:

使用Wpdb尝试从表中获取列。我尝试的代码是:

<?php
global $wpdb;
$sqlq2 = 'SELECT fk_id FROM `wp_productssku_mapping`';
$result = $wpdb->get_results($sqlq2);
foreach($result as $row)
 {
print_r($row->fk_id); 
 }
?>

这会在到达

时导致空白页面
xyz.com/wp-content/themes/sometheme/name.php

错误日志:空白(没有与此相关的错误。)

可能存在什么问题?

更新

enter image description here

我在表中有以下数据,我需要逐个获取它并在curl命令中使用它。

1 个答案:

答案 0 :(得分:0)

快速阅读wpdb class reference后,我将您的上述方案与以下代码段一起使用:

public @ResponseBody void export(@PathVariable final String whatEver,
        @RequestParam("type") final String type, @RequestParam("text") final String text,
        final HttpServletRequest request)

我相信它不适合你的原因是你没有指定输出类型,阅读上面的链接是你的选择:

    global $wpdb;        
    $users = $wpdb->get_results( 'SELECT * FROM wp_users', ARRAY_A );

    foreach ($users as $user) {
        print_r($user);
    }

我个人更喜欢使用关联数组,但是你想要对象方法,然后在get_results中将OBJECT指定为第二个参数,如下所示:

One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information.
OBJECT - result will be output as a numerically indexed array of row   objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as a numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

它应该没问题。

使用OBJECT完成下面的解决方案。只需换出变量名和表名。

$users = $wpdb->get_results( 'SELECT * FROM wp_users', OBJECT );

我也注意到你试图直接执行你的php文件,这在wordpress中是一个很大的不。最好创建一个针对像admin_post这样的wordpress钩子的函数回调。机会是你从未在wordpress实例中实际运行的代码,因此$ wpdb很可能是null。

阅读以下内容:https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)关于如何在钩子上回调。您可以使用admin_post创建发布和获取请求。