i am fetching records from two tables at once. one is a questions table and the other is an options table, each question has many options,im running the below code but it echos nothing, no error, no result.
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="questions">
<?php
$results = $wpdb->get_results( "SELECT * FROM wp_dbquestions"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo"<p>$results->text</p>";
}
$results1 = $wpdb->get_results( "SELECT * FROM wp_questoptions WHERE question_id=1"); // Query to fetch data from database table and storing in $results
if(!empty($results1)) {
foreach($results1 as $row){
echo"<form class="options">";
echo"<input class="option" type="radio">$row->text <br>";
echo"<input class="option" type="hidden">$row->score <br>";
echo"</form>";
}
?>
</div>
</div>
</div>
</div>
While testing/debugging you can add this
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
on top of your php file to see all errors.
If you want to use the Wordpress database you have to "declare $wpdb as a global variable using the global keyword, or use the superglobal $GLOBALS"
// 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );
// 2nd Method - Utilizing the $GLOBALS superglobal. Does not require global keyword ( but may not be best practice )
$results = $GLOBALS['wpdb']->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_id = 1", OBJECT );
From:
https://codex.wordpress.org/Class_Reference/wpdb
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.