Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Is the $result NULL, false or an empty array? Are you sure wp_frm_item_metas is a valid database name to be asking`$wpdb` of? Try echoing out $wpdb->wp_frm_item_metas and see what it gives you, check that the table is in the database and is called that way. Turn on error output for both $wpdb. –  soulseekah Oct 20 '11 at 6:38

4 Answers 4

up vote 3 down vote accepted

I believe for your foreach function should be like below:

foreach ($result as $item){
            $eventname= $item->meta_value;
        }
share|improve this answer
1  
This answer was accepted but strikes me as wrong. $eventname is going to be only the last item in $result whether the query returns 1 or 100 elements. –  s_ha_dum Apr 8 at 12:53

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.

share|improve this answer

You got an error on your code, the $eventname should contain $item->meta_value

share|improve this answer
    
The accepted answer, though flawed in its own way, does point out why $eventname does not contain $item->meta_value. –  s_ha_dum Apr 8 at 12:58

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..

http://i54.tinypic.com/bemom.jpg

share|improve this answer
    
This does not appear to be an "Answer" but another question or perhaps an edit to the original question. Please do not abuse the "Answer" box by posting further questions. An "Answer" should be a solution to the problem posted in the question. –  s_ha_dum Apr 8 at 12:45
    
Also, why did you start with a question that uses Core functions and methods and end up using no Core functions at all? –  s_ha_dum Apr 8 at 12:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.