Retrieving a Value from a wp-database

why my sql statement is not returning any value.. is this wrong?

<?php

        global $wpdb;
        $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->wp_frm_item_metas"));
        foreach ($result as $item){
            $eventname= $result->meta_value;
        }

        ?>

        <h2><?php echo $eventname;?></h2>

What I’m doing is, I’m displaying the data stored in database using formidable form.. (not pro).. Is there something wrong with my code?

Related posts

Leave a Reply

4 comments

  1. There are several problems with your code.

    1. Unless you, or a plugin, has added wp_frm_item_metas to $wpdb,
      $wpdb has not idea what $wpdb->wp_frm_item_metas is. You will
      get an “Undefined Property” error, and your query won’t work.

      You can’t just use $wpdb-> plus any table name. That won’t work.
      You have to add the property to $wpdb, which isn’t that hard to
      do
      .

    2. Your foreach is wrong. You are Looping over $results but at each
      iteration you need to be accessing $item, not $result. Like so:

      foreach ($result as $item){
        echo $item->meta_value;
      }
      
    3. However, your query contains no limiting login– no WHERE clause–
      so it will return every row in the wp_frm_item_metas table. So,
      either you need this…
      foreach ($result as $item){
      $eventname[] = $item->meta_value;
      } …, which doesn’t make sense given your attempt to echo $eventname;
    4. Or both your query and your choice of $wpdb method is wrong. It
      looks to me like you need something closer to this:

      $eventname = $wpdb->get_var(
        $wpdb->prepare(
          "SELECT meta_value* FROM $wpdb->wp_frm_item_metas WHERE something = %s",
          'something'
        )
      );
      

      You can now echo $eventname without the bother of the loop.

    5. Notice how I altered the prepare method call. $wpdb->prepare
      must have two arguments. Your code would have failed at that point as well.

    For reference: https://codex.wordpress.org/Class_Reference/wpdb

    And please enable debugging while you work. You would have spotted much of this had you followed that simple rule.

  2. ok I got it thanks a lot..

    now I tried to display the output in a proper way like this..
    Column 1 – Event name (with clickable link that when clicked goes to a page that shows the single event on a page with details)
    Column 2 – Date of event
    Column 3 – City,State.(place)

    here is my code

    $query = "SELECT * FROM wp_frm_item_metas where field_id IN (8,13, 12, 11 )";
                        $result = @mysql_query($query) or die (mysql_error());
                        ?><table border="1" colspan="3">
                        <th>Event</th>
                        <th>Date</th>
                        <th>Venue<?
                        $i=1;
                        while($i <= 1)
                        { echo "<tr>";
                        while($row = mysql_fetch_array($result)){
    
    
    
                                if($row['field_id'] == 8){
                                        $title = $row['meta_value'];
    
                                }
    
                                else if($row['field_id'] == 13){
                                        $link = $row['meta_value'];
                                echo "<td><a href=$link target=_blank> $title </a></td>";
    
                                }
    
                                else if($row['field_id'] == 12){
                                        $date=$row['meta_value'];
                                        echo "<td>$date</td>";
                                }
    
    
    
                                else {
                                        $place=$row['meta_value'];
    
                                        echo "<td>$place</td>";
                                }
    
    
    
                                $i++;
    
    
                        }      
    
    
    
                        echo "</tr>";
                                }
                                ?>
                                </table>
    

    but the output is really confusing.. in the first column is the venue..
    the second is correct the date.. and the third is the title..
    Why the display is like that?

    Here’s the structure of the database:

    enter image description here