怎么扩展mySQLI_Result并添加新方法
原学程将引见若何扩大mySQLI_Result并添减新办法的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
Mysqli_Result类有fetch_all()
办法以言数组(现实上是两维数组,由于言曾经是字段数组)的情势从成果散中1次前往多个言。别的,成果散具备以对于象情势前往单言的办法fetch_object()
。
如今,我想扩大mysqli_Result类并添减新办法fetch_objects()
(单数),该办法将前往对于象的联系关系数组,个中联系关系键是记载ID[id=>Object()]。固然,为了前往扩大的成果散,我借须要扩大mysqli类并笼罩它的Query()办法。然则我没有晓得mysqli以及前往的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;
}
php>
我在mysqli_result的推举谜底脚册中找到了这1面,并对于其停止了1些修补。不外我出有试过。
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_Result并添减新办法的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。