It’s very clear that your file is not executing PHP functions, and the problem probably is because you are saving this file as something.html
, storing it inside your theme or WordPress installation directory.
What you can do, is to wrap this as a function or shortcode, and then create a page and use that shortcode or function inside it.
Here is a simple example to develop shortcode from your query:
add_shortcode('my-shortcode', 'create_my_shortcode');
function create_my_shortcode(){
global $wpdb;
$result = $wpdb -> get_results ( "
SELECT 'VillageLeadersName'
FROM wp_villageleaderdb
WHERE VillageID = 'V001' AND VillageLeaderPosition = 'Sarpanch' LIMIT 0, 100
" );
return $result[0][1] . "<br />";
}
Put this code inside your theme’s functions.php
file, and then head over to New > Page
and create a page. Afterward, you can use this shortcode inside your page:
[my-shortcode]
This will execute and output your code. Also note that you can not echo
content in a shortcode, you must return
it.
Another approach is to simply wrap it as a function without creating a shortcode, and then call that function inside your templates.
function create_my_shortcode(){
// Your code here
}
Now, make a copy of your theme’s page.php
, rename it to page-whatever.php
and modify it the way you like. After you finished editing, use create_my_shortcode()
anywhere you want. In this method you can echo
the content too.
Afterward, you can use this template while creating a new page in the back-end.
UPDATE
As stated in your comments, the results might be an array. To print the array, you can use a foreach like this:
$data="";
foreach( $result as $single_result ){
$data .= $single_result;
}
return $data. $row['VillageLeadersName'] . "<br />";