Wordpress如何从数据库中获取记录
Wordpress how to fetch records from database
我正在同时从两个 table 中获取记录。
- 一个是问题table
- 另一个是选项table
每个问题都有很多选项,我是运行下面的代码,但它什么也没回显,没有错误,没有结果。
<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>
虽然 testing/debugging 你可以添加这个
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
在您的 php 文件之上查看所有错误。
如果您想使用 Wordpress 数据库,您必须 "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 );
发件人:
https://codex.wordpress.org/Class_Reference/wpdb
我正在同时从两个 table 中获取记录。
- 一个是问题table
- 另一个是选项table
每个问题都有很多选项,我是运行下面的代码,但它什么也没回显,没有错误,没有结果。
<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>
虽然 testing/debugging 你可以添加这个
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
在您的 php 文件之上查看所有错误。
如果您想使用 Wordpress 数据库,您必须 "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 );
发件人:
https://codex.wordpress.org/Class_Reference/wpdb