php – Retrieving and displaying data from WordPress database-ThrowExceptions

Exception or error:

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 my table
$retrieve_data = $wpdb->get_results( "SELECT * FROM wpex_programma" );
?>
<!--This will display my files-->
<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?

My database structure:

enter image description here

How to solve:

Here is an example code that will get the data and then display it

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 $table_name" );
?>
<ul>
foreach ($retrieve_data as $retrieved_data){ ?>
<li><?php echo $retrieved_data->id;?></li>
<li><?php echo $retrieved_data->naam;?></li>
<li><?php echo $retrieved_data->as_many_columns_as_you_have;?></li>
<?php 
}
?>

Class Reference/wpdb

  1. Make sure you have a database created,
  2. Make sure you are adding correct table_name,
  3. Make sure you have data inserted in table.

Answer:

You can check the data from your database table. In PHP MySQL database. you can find the table name after the prefix of the table which you have decided while installing the WordPress. there should be your column name exact. with the table name.

Answer:

It seems that your code is correct but problem is in prefix in table name.

$table_name = $wpdb->prefix . “wpex_programma”;

wpex_ is your table prefix name that you can get by using $wpdb->prefix so just remove wpex_ from table name.

$table_name = $wpdb->prefix . "programma";

or

$table_name = "wpex_programma";

if you have added different custom prefix name rather than have in other default tables, in this case,

$wpdb->prefix does not work for your custom table.

You can check wordpress prefix table name in wp-config.php

$table_prefix = 'wpex_';

and this name is different from your custom tables prefix than use like this:

$table_name = "wpex_programma";

and is it same in both your custom table and wordpress default tables than use like this:

$table_name = $wpdb->prefix . "programma";

Leave a Reply

Your email address will not be published. Required fields are marked *