<?php
// Add this code to your theme's functions.php file or a custom plugin
// Function to fetch data from a MySQL table and display it
function display_data_from_table() {
global $wpdb; // Access the WordPress database object
// Define your custom table name (replace 'your_table_name' with your actual table)
$table_name = $wpdb->prefix . 'your_table_name'; // Prefix adds wp_ or your custom prefix
// Prepare SQL query to fetch data
$results = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);
// Check if results are found
if ($results) {
// Start building the HTML table to display data
$output = '<table>';
$output .= '<thead><tr>';
// Display table headers (adjust to match your table columns)
foreach (array_keys($results[0]) as $column_name) {
$output .= '<th>' . esc_html($column_name) . '</th>';
}
$output .= '</tr></thead><tbody>';
// Loop through each row and display data
foreach ($results as $row) {
$output .= '<tr>';
foreach ($row as $column_value) {
$output .= '<td>' . esc_html($column_value) . '</td>';
}
$output .= '</tr>';
}
$output .= '</tbody></table>';
} else {
$output = '<p>No data found.</p>';
}
return $output;
}
// Register the shortcode to display the data
add_shortcode('display_data', 'display_data_from_table');
Sample Page
This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
Hi there! I’m a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)
…or something like this:
The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.
As a new WordPress user, you should go to your dashboard to delete this page and create new pages for your content. Have fun!