也就是说,准备好的声明如下:

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类的正确属性。

有帮助吗?

解决方案

其他提示

感谢您的更正。当你在'select *'面前别无选择时,它非常有用 S上。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top