$ wpdb使用SELECT查询不返回任何结果

时间:2016-07-20 07:37:28

标签: php wordpress

我在wordpress数据库上创建了一个自定义表。我从我的自定义插件插入行,但我从这个表中选择。选择返回始终没有。 工作正常的插件是

   <?php
  global $wpdb;
     $wpdb->insert( 
'wp_SimParts', 
array( 
    'ProductName' => 'testname', 
    'ProductPrice' => 123,
            'ProductDescription' => 'testdescription',  
    'ProductImage' => 'testimage',
    'CategoryID' => 3
   ), 
 array( 
    '%s', 
    '%f',
    '%s', 
    '%s',
    '%d'
   ) 
 );

  ?>

当我尝试从这张桌子中选择时,我什么都没带

<?php       
    global $wpdb;
    if(isset($_POST['search_product']))
    {
    $mytestproducts = $wpdb->get_results(
    "
    SELECT id, ProductName
    FROM $wpdb->wp_SimParts

    "
    );
       ?>
     <?php
    foreach($mytestproducts as  $mytestproduct)
    {
     ?>         
        <tr>
   <?php
        echo"<td>".$mytestproduct->ProductName."</td>";
        echo "<td>".$mytestproduct->id."</td>";
      ?>
        </tr>
      <?php
    }
    }
    ?>  

如果我尝试从帖子中进行选择,我通常会得到结果。

1 个答案:

答案 0 :(得分:1)

$ wpdb 未设置对象变量 $ wpdb-&gt; wp_SimParts 。这是表名。

将您的查询更改为:

$mytestproducts = $wpdb->get_results(
  "SELECT id, ProductName
  FROM wp_SimParts"
);
相关问题