如何从$ wpdb-> get_results
4 个回答
- 投票数
-
- 2011-07-19
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) { // each column in your row will be accessible like this $my_column = $row->column_name;}
更多信息此处
foreach( $wpdb->get_results("SELECT * FROM your_table_name WHERE id LIKE' . $id . ';") as $key => $row) { // each column in your row will be accessible like this $my_column = $row->column_name;}
More info here
-
不确定这是否是正确的方法.为了安全起见,我认为应该将结果赋给变量并对其使用foreach.例如.$ results=$ wpdb->get_results($ sql);然后使用foreach($ results作为$ value).not sure if this is the right way though. I think one should get the result to a variable and use foreach on that, to be safe. E.g. $results = $wpdb->get_results($sql); and then use foreach($results as $value).
- 2
- 2013-09-13
- Gogol
-
在这种情况下,它实际上并不重要,因为它返回数组,对象或null,因此不存在获取"循环不友好"资源的任何风险.那表示您可能想再次遍历它们,寻找其他东西,如果可以的话,一定要存储它.不要查询两次shouldn't really matter in this instance since it returns array, object or null, there shouldn't be any risk of getting a "loop unfriendly" resource. that said you may want to loop through them again for something else, and if so definitely store it. don't query twice
- 0
- 2017-10-29
- Garet Claborn
-
- 2011-07-19
始终尝试WordPress Codex: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
本质上给出了默认语法,此处的$ row变量是一个包含您的结果的对象.您可以交替指定结果的类型(数字数组,关联数组).
假设只有一个结果,那么$ row->id和$ row->name应该会为您提供信息.
如果返回不只一个结果,则希望遍历对象中的条目.
如果只希望返回一行,请尝试使用$ wpdb->get_row http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row
Always Try the WordPress Codex: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
Essentially given the default syntax, the variable $row here is an object containing your results. You could alternately specify the TYPE of result (numeric array, associative array).
Assuming just one result, then $row->id and $row->name should give you the information.
If you get back more than one result, you'd want to loop over the entries in the object.
If you are expecting just one row back, then try using $wpdb->get_row http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row
-
- 2017-10-30
用作关联数组:
$obj=[]; $rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_A); foreach($rows as $row){ $obj=$row; break; } // $obj is now the selected row if a match was found
用法
$something = $obj['column_name']; foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL . '<br />';
要获取其他格式,只需根据ARRAY_A ">
$wpdb->get_results()
的文档. Pippin的答案适用于大多数对象.要将一行用作数字索引数组
$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_N); foreach($rows as $row){ $obj=$row; break; } //Usage foreach($obj as $col_value) echo $col_value . ' ';
要使用其键是数据库中的主键的数组中的一行(通常是
id
列).可能比关联数组方法更有效.$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , OBJECT_K); $obj = $rows[ $obj_id ]; //Usage $something = $obj->column_name; //Remember you can loop over objects too foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL;
To use as an associative array:
$obj=[]; $rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_A); foreach($rows as $row){ $obj=$row; break; } // $obj is now the selected row if a match was found
Usage
$something = $obj['column_name']; foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL . '<br />';
To get other formats, simply change
ARRAY_A
based on the documentation for$wpdb->get_results()
. Pippin's answer is appropriate for most object use.To use one row as an numerically indexed array
$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , ARRAY_N); foreach($rows as $row){ $obj=$row; break; } //Usage foreach($obj as $col_value) echo $col_value . ' ';
To use one row in an array whose keys are the primary key from your database(often an
id
column). Possibly more efficient than the associative array method.$rows = $wpdb->get_results( 'SELECT * FROM `tbl_name` WHERE `id` = '.$obj_id , OBJECT_K); $obj = $rows[ $obj_id ]; //Usage $something = $obj->column_name; //Remember you can loop over objects too foreach($obj as $col => $val) echo $col . ': ' . $val . PHP_EOL;
-
- 2018-03-15
此代码最适合我:
global $wpdb; $table_name = "my_table_name"; $myrows = $wpdb->get_results( "SELECT `id`, `name` FROM ".$table_name); foreach ($myrows as $details) { echo $details->id; echo $details->name;}
This code work perfect for me:
global $wpdb; $table_name = "my_table_name"; $myrows = $wpdb->get_results( "SELECT `id`, `name` FROM ".$table_name); foreach ($myrows as $details) { echo $details->id; echo $details->name;}
我有以下内容:
如何从$ row中获取名为"id"和"name"的列?