最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Retrieving and Displaying Data From a Table

programmeradmin2浏览0评论

At the moment, I have created a code that retrieves data from the database and displays it. However, for some reason, I cannot see the files I want to retrieve on my page. My goal is that the data gets retrieved from the database, and is displayed on the webpage. I do not need to make a connection with the database since Wordpress does that automatically.

My code:

<?php

global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "wpex_programma";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM wpex_programma; " );
print_r($retrieve_data);
?>
<ul>
<?php foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php 
}
?>
</ul>

My question: the data is not shown and I believe it is not retrieved. How can I fix that? The table name is correct, and I have data in my database tabel. The print_r($retrieve_data)returns the following: Array ( [0] => stdClass Object ( [id] => 35 [naam] => /public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload/Country Kickin 1.mp3 ) )

My query log:

My database structure:

New:

<?php

global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "wpex_programma";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM wpex_programma; " );
print_r($retrieve_data);
?>
<ul>
<?php foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->id;?></li>
<li><?php echo $retrieved_data->naam;?></li>
<?php 
}
?>
</ul>

At the moment, I have created a code that retrieves data from the database and displays it. However, for some reason, I cannot see the files I want to retrieve on my page. My goal is that the data gets retrieved from the database, and is displayed on the webpage. I do not need to make a connection with the database since Wordpress does that automatically.

My code:

<?php

global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "wpex_programma";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM wpex_programma; " );
print_r($retrieve_data);
?>
<ul>
<?php foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php 
}
?>
</ul>

My question: the data is not shown and I believe it is not retrieved. How can I fix that? The table name is correct, and I have data in my database tabel. The print_r($retrieve_data)returns the following: Array ( [0] => stdClass Object ( [id] => 35 [naam] => /public_html/wp/wp-content/plugins/AbonneerProgrammas/FilesUpload/Country Kickin 1.mp3 ) )

My query log:

My database structure:

New:

<?php

global $wpdb;
// this adds the prefix which is set by the user upon instillation of wordpress
$table_name = $wpdb->prefix . "wpex_programma";
// this will get the data from your table
$retrieve_data = $wpdb->get_results( "SELECT * FROM wpex_programma; " );
print_r($retrieve_data);
?>
<ul>
<?php foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->id;?></li>
<li><?php echo $retrieved_data->naam;?></li>
<?php 
}
?>
</ul>

Share Improve this question edited Mar 12, 2020 at 13:08 Tom J Nowell 61.1k7 gold badges79 silver badges148 bronze badges asked Mar 12, 2020 at 10:28 Parsa_237Parsa_237 1117 bronze badges 14
  • That looks OK to me. Note that your $table_name variable will be "wp_wpex_programma", because you're prefixing $wpdb->prefix, but you're not using that anyway. Can you use your database tools to check the query is being run on the database OK and returning results? Or e.g. the Query Monitor plugin? – Rup Commented Mar 12, 2020 at 10:46
  • Hi @Rup, yes the query works fine on my database when I run it. Should I just delete: $table_name = $wpdb->prefix . "wpex_programma";? – Parsa_237 Commented Mar 12, 2020 at 10:48
  • You can delete the line but it shouldn't matter either way. I meant when WordPress runs the query, e.g. the MySQL query log or a profiler to check the actual SQL that is being run on the database, in case it's changed the SQL string somehow - but I don't think it does. I'd try the Query Monitor plugin though if you haven't already. – Rup Commented Mar 12, 2020 at 10:50
  • @Rup, I will download the plugin and let you know what it tells me – Parsa_237 Commented Mar 12, 2020 at 10:53
  • @Rup, I added a pic to my question. Is that the query log? – Parsa_237 Commented Mar 12, 2020 at 10:58
 |  Show 9 more comments

1 Answer 1

Reset to default 2

You have this:

<li><?php echo $retrieved_data->column_name;?></li>
<li><?php echo $retrieved_data->another_column_name;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>

Here, the code tries to output the data in the column_name column, but there is no column in the table by that name.

Likewise, there is no another_column_name or as_many_columns_as_you_have columns in your table.

We know that the query pulls in the data, because when you used print_r we could see the data. We also know that when we did that we didn't see fields named column_name, we saw fields named id and naam.

After a little digging, it looks like you took a generic example from here:

https://wordpress.stackexchange/a/54641/736

But didn't make any modifications to match your table. The example wasn't a copy paste work anywhere code block, such a thing does not exist for what you want.

Your table has id and naam columns, these are what the code should be using, not another_column_name or as_many_columns_as_you_have

发布评论

评论列表(0)

  1. 暂无评论