2016-05-31 55 views
0

问题:wpdb get_results打印表格列时没有输出

使用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命令使用它。

+0

尝试print_r($ result);在foreach循环之前 –

+0

@RaviKumar没有变化。 –

+0

尝试print_r($ result);出口;并在按下按钮后查看底部 –

回答

0

wpdb class reference快速阅读后,我得到了你的上述方案与下面的代码段的工作:

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

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

我相信为什么它不为你工作的原因是因为你没有指定输出类型,阅读上述这些链接是你的选择:

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. 

我个人更喜欢用关联数组工作,但你想要的对象的方法,然后指定对象作为像这样的get_results你的第二个参数:

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

它应该很好。

完整的解决方案下面有OBJECT。只需换出变量名称和表名。

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

    foreach ($users as $user) { 
     print_r($user->ID); 
    } 

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

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

+0

感谢您的答案,如果您可以检查有问题的更新,获取列数据是用于一个一个的curl命令.. –

+0

上面提供的解决方案不起作用 –

+0

你有没有试过$ result = $ wpdb-> get_results($ sqlq2,OBJECT);然后使用echo $ row-> fk_id? – Nebri