也就是说,使用如下准备好的语句:
select col1,...coln from table where col3 = ?
我相信我可以使用 $mysqli->field_count 来获取返回的列数(没有尝试过)。
但是有没有办法将每个列名链接到 bind_results 中返回的值?我总是可以尝试从命令本身解析列名,但这不是我想走的路。
语境:
我希望能够定义一个 PHP 接口,将类属性映射到从数据库返回的列名。所以给定
class A implements IColumnMapped{
public $prop1, $prop2; private $map;
public function __construct() {
$map = array();
$map['col1'] = & $this->prop1;
$map['col2'] = & $this->prop2;
}
public function getMap() { return $this->map;}
}
和
$mysqli->prepare("select col0, col1, col2, col3 from table");
我可以使用 bind_results 然后做类似的事情(伪代码)
for($resultColumns as $columnName) {
if(isset($map[$columnName])) $map[$columnName] = $results[$columnName];
}
只取出我需要的两列(col1 和 col2)并将它们分配给 A 类的正确属性。