如何扩展 mysqli_result 并添加新方法
How to extend mysqli_result and add new method
Mysqli_result class 有 fetch_all()
方法 return 以行数组的形式一次从结果集中获取多行(实际上这是二维数组,因为行已经是字段数组)。结果集也有方法 fetch_object()
return 以对象的形式生成单行。
现在我想扩展 mysqli_result class 并添加新方法 fetch_objects()
(复数),这将 return 一个对象的关联数组,其中关联键是记录 ID [id=>Object()]。当然,我也需要扩展 mysqli class 并覆盖它的 query() 方法,以便 return 我的扩展结果集。但是我不知道如何在 mysqli 和 returned mysqli_result 之间传输连接和查询参数。
这应该是这样工作的:
$db=new ReallyMySQLi(...); //initiating my mysqli
$myrst=$db->query($SQL); //returned my extended result
//fetch_objects() returning associative array of objects
//with record ID as associative key
foreach($myrst->fetch_objects() as $id=>$object)
{
echo 'Customer ID:'.$id; //or $object->id;
echo 'Customer firstname:'.$object->firstname;
echo 'Customer lastname:'.$object->lastname;
}
我在 mysqli_result 的 PHP 手册中找到了它并进行了一些修改。不过我没试过。
class Database_MySQLi extends MySQLi
{
public function query($query)
{
$this->real_query($query);
return new Database_MySQLi_Result($this);
}
}
class Database_MySQLi_Result extends MySQLi_Result
{
public function fetch_objects()
{
$rows = array();
while($row = $this->fetch_object())
{
$rows[$row->id] = $row;
}
return $rows;
}
}
用法是
$db=new Database_MySQLi(...); //initiating my mysqli
$myrst=$db->query($SQL); //returned my extended result
//fetch_objects() returning associative array of objects
//with record ID as associative key
foreach($myrst->fetch_objects() as $id=>$object)
{
echo 'Customer ID:'.$id; //or $object->id;
echo 'Customer firstname:'.$object->firstname;
echo 'Customer lastname:'.$object->lastname;
}
抱歉,我已经两年没用 mysqli 作为 mysql 驱动程序了。但我为你推荐这个。
你可以选择PDO,PDO作为多堆数据库驱动实在是太酷了
和laraveleloquent一样,学说都以PDO为基础。
$pdo = new PDO('sqlite:db.sqlite');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
祝你好运!
Mysqli_result class 有 fetch_all()
方法 return 以行数组的形式一次从结果集中获取多行(实际上这是二维数组,因为行已经是字段数组)。结果集也有方法 fetch_object()
return 以对象的形式生成单行。
现在我想扩展 mysqli_result class 并添加新方法 fetch_objects()
(复数),这将 return 一个对象的关联数组,其中关联键是记录 ID [id=>Object()]。当然,我也需要扩展 mysqli class 并覆盖它的 query() 方法,以便 return 我的扩展结果集。但是我不知道如何在 mysqli 和 returned mysqli_result 之间传输连接和查询参数。
这应该是这样工作的:
$db=new ReallyMySQLi(...); //initiating my mysqli
$myrst=$db->query($SQL); //returned my extended result
//fetch_objects() returning associative array of objects
//with record ID as associative key
foreach($myrst->fetch_objects() as $id=>$object)
{
echo 'Customer ID:'.$id; //or $object->id;
echo 'Customer firstname:'.$object->firstname;
echo 'Customer lastname:'.$object->lastname;
}
我在 mysqli_result 的 PHP 手册中找到了它并进行了一些修改。不过我没试过。
class Database_MySQLi extends MySQLi
{
public function query($query)
{
$this->real_query($query);
return new Database_MySQLi_Result($this);
}
}
class Database_MySQLi_Result extends MySQLi_Result
{
public function fetch_objects()
{
$rows = array();
while($row = $this->fetch_object())
{
$rows[$row->id] = $row;
}
return $rows;
}
}
用法是
$db=new Database_MySQLi(...); //initiating my mysqli
$myrst=$db->query($SQL); //returned my extended result
//fetch_objects() returning associative array of objects
//with record ID as associative key
foreach($myrst->fetch_objects() as $id=>$object)
{
echo 'Customer ID:'.$id; //or $object->id;
echo 'Customer firstname:'.$object->firstname;
echo 'Customer lastname:'.$object->lastname;
}
抱歉,我已经两年没用 mysqli 作为 mysql 驱动程序了。但我为你推荐这个。
你可以选择PDO,PDO作为多堆数据库驱动实在是太酷了
和laraveleloquent一样,学说都以PDO为基础。
$pdo = new PDO('sqlite:db.sqlite');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
祝你好运!