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?
I believe for your foreach function should be like below:
There are several problems with your code.
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 willget 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 todo.
Your
foreach
is wrong. You are Looping over$results
but at eachiteration you need to be accessing
$item
, not$result
. Like so: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;
Or both your query and your choice of
$wpdb
method is wrong. Itlooks to me like you need something closer to this:
You can now
echo $eventname
without the bother of the loop.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.
You got an error on your code, the $eventname should contain $item->meta_value
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
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: