I have an array of the record retrieved from a SQL server database stored in an object $result. I will like to access each item in the array object and print it out to the screen. How can I achieve this? My attempt is below
我有一个从存储在对象$ result中的SQL服务器数据库中检索到的记录数组。我想访问数组对象中的每个项目并将其打印到屏幕上。我怎样才能做到这一点?我的尝试如下
$result = $DB->get_records_sql("SELECT rawname, ID FROM mdl_tag where tagType = 'institution'");
$result = array();
foreach ($result as $ $value) {
echo $value;
);
}
1 个解决方案
#1
1
Here is the correct code:
这是正确的代码:
// Get results from table 'tag', where 'tagtype' matches 'institution', with no sorting and returning fields 'id' and 'rawname'.
$results = $DB->get_records('tag', ['tagtype' => 'institution'], '', 'id, rawname');
foreach ($results as $value) {
echo $value->rawname;
}
So, there are a number of issues:
所以,有很多问题:
- Use get_records, unless you really need to use get_records_sql.
- If you do need to use get_records_sql, then the table is called {tag} not mdl_tag, otherwise you'll run into problems on any server that uses a prefix other than 'mdl_' (e.g. when running phpunit or behat tests).
- Writing $result = array(); immediately before looping through the result will throw away the details from the database and replace them with an empty array
- The results from get_records (or get_records_sql) are an array containing objects, each of which contains the values for the requested fields, so you need to access the individual value, not try to echo the entire object
- All database fields in Moodle are lowercase, so 'tagtype' not 'tagType', 'id' not 'ID'.
使用get_records,除非你真的需要使用get_records_sql。
如果你确实需要使用get_records_sql,那么该表被称为{tag}而不是mdl_tag,否则你将在任何使用“mdl_”之外的前缀的服务器上遇到问题(例如,当运行phpunit或behat tests时)。
写$ result = array();在循环结果之前,将立即丢弃数据库中的详细信息并将其替换为空数组
get_records(或get_records_sql)的结果是一个包含对象的数组,每个对象都包含所请求字段的值,因此您需要访问单个值,而不是尝试回显整个对象
Moodle中的所有数据库字段都是小写的,因此'tagtype'不是'tagType','id'不是'ID'。
#1
1
Here is the correct code:
这是正确的代码:
// Get results from table 'tag', where 'tagtype' matches 'institution', with no sorting and returning fields 'id' and 'rawname'.
$results = $DB->get_records('tag', ['tagtype' => 'institution'], '', 'id, rawname');
foreach ($results as $value) {
echo $value->rawname;
}
So, there are a number of issues:
所以,有很多问题:
- Use get_records, unless you really need to use get_records_sql.
- If you do need to use get_records_sql, then the table is called {tag} not mdl_tag, otherwise you'll run into problems on any server that uses a prefix other than 'mdl_' (e.g. when running phpunit or behat tests).
- Writing $result = array(); immediately before looping through the result will throw away the details from the database and replace them with an empty array
- The results from get_records (or get_records_sql) are an array containing objects, each of which contains the values for the requested fields, so you need to access the individual value, not try to echo the entire object
- All database fields in Moodle are lowercase, so 'tagtype' not 'tagType', 'id' not 'ID'.
使用get_records,除非你真的需要使用get_records_sql。
如果你确实需要使用get_records_sql,那么该表被称为{tag}而不是mdl_tag,否则你将在任何使用“mdl_”之外的前缀的服务器上遇到问题(例如,当运行phpunit或behat tests时)。
写$ result = array();在循环结果之前,将立即丢弃数据库中的详细信息并将其替换为空数组
get_records(或get_records_sql)的结果是一个包含对象的数组,每个对象都包含所请求字段的值,因此您需要访问单个值,而不是尝试回显整个对象
Moodle中的所有数据库字段都是小写的,因此'tagtype'不是'tagType','id'不是'ID'。