user2340890
user2340890

Reputation: 25

$wpdb returns no results with SELECT query

I have created a custom table on a wordpress database. I am inserting rows from my custom plugin but I canot select from this table. Select return always nothing. The insert that is working fine is

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

  ?>

When I try to select from this table I am taking nothing

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

If I try to select from posts I am taking normally results.

Upvotes: 2

Views: 1205

Answers (1)

user6086593
user6086593

Reputation:

Object variable $wpdb->wp_SimParts is not set for $wpdb. It is table name.

Change your query to this:

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

Upvotes: 1

Related Questions