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
使用情况
$something = $obj[\'column_name\'];
foreach($obj as $col => $val)
echo $col . \': \' . $val . PHP_EOL . \'<br />\';
要获取其他格式,只需更改
ARRAY_A
基于
documentation for $wpdb->get_results()
. 皮平的回答适用于大多数对象使用。
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(通常是
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;