从受保护的属性获取值 [英] Get values from protected property
本文介绍了从受保护的属性获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在访问受保护的属性时遇到问题。我正在尝试查询wordpress的数据库表。以下是我的代码:
I am having issues with accessing the protected property. I am trying to query a database table of wordpress. Following is my code:
<?php
$table_name = 'wp_wp_pro_quiz_question';
$current_quiz_id = $wpdb->get_var("SELECT max(quiz_id) as quiz_id FROM $table_name");
$results = $wpdb->get_results( "SELECT * FROM $table_name where quiz_id = $current_quiz_id");
if(!empty($results)){
foreach($results as $row){?>
// print_r($row->answer_data);
print_r(unserialize($row->answer_data));
$answer_data = unserialize($row->answer_data);
$answers = array();
foreach($answer_data as $obj) {
$answers[] = $obj->get_answer;
}
print_r($answers);
}
?>
print_r(unserialize($ row-> answer_data))给出以下结果:
print_r(unserialize($row->answer_data)) gives following result:
Array (
[0] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 26 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => 1 [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => )
[1] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 6 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => )
[2] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 4 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => )
[3] => WpProQuiz_Model_AnswerTypes Object ( [_answer:protected] => 16 [_html:protected] => 1 [_points:protected] => 1 [_correct:protected] => [_sortString:protected] => [_sortStringHtml:protected] => 1 [_mapper:protected] => ) )
我使用 get 访问受保护的字段。我将所有值都推送到数组$ answers,但所有值都为null。
print_r($ answers)的结果:
数组([0] => [1] => [2] => [3] =>)
I used get to access the protected field. I pushed all the values to the array $answers but i am getting all values null. Result of print_r($answers): Array ( [0] => [1] => [2] => [3] => )
我在这里做什么错了?
谢谢
推荐答案
用一个替换两个选择:
SELECT * FROM $table_name
ORDER BY quiz_id DESC LIMIT 1
然后将 *
替换为你想要的。
Then replace *
by the list of columns that you want.
这篇关于从受保护的属性获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文