数据库结果作为对象或数组?

Jon*_*han 11 php mysql

这个问题类似于PHP中的Mysql结果 - 数组或对象?但是,我的问题扩展到那里讨论的内容.

我正在尝试确定哪种格式更适合处理数据库结果:对象或数组.我不关心性能(据我所知,它没有什么区别).我的重点更多的是显示结果 - 不创建,更新或删除它们.

到目前为止,我总是使用像mysqli_fetch_objectPDO这样的函数来使用对象fetchObject.这通常很好用,直到我开始做连接.连接导致奇怪的对象,这些对象是来自两个或多个表的字段的混合.我的代码很快就开始变得混乱.

我应该注意,我正在分配特定的类名,而不是坚持默认stdClass.我这样做,以便我可以访问我在我的类中创建的任何帮助方法.例如:

foreach ($members as $member)
{
    echo $member->full_name();
    echo $member->age();
}
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,我正在考虑为我的所有数据库结果移动到数组.从我读过的其他人也这样做.但是,这让我没有简单的方法来访问我的帮助方法.

使用上面的例子,我想我可以输出名字和姓氏而不是使用full_name()方法,这不是什么大问题.至于age()方法,我想我可以创建一个通用实用程序类并将其放在那里.

我的问题:

  1. 如果使用(模型)对象,如何处理连接?
  2. 如果使用数组,那么如何处理辅助方法?

eth*_*nny 7

我总是使用对象 - 但我不直接从查询中放入数据.使用'set'函数我创建了布局,因此避免了连接和命名冲突的问题.对于'full_name'示例,我可能会使用'as'来获取名称部分,在对象中设置每个部分并提供'get_full_name'作为成员fn.

如果你有野心,你可以在'get_age'中添加各种各样的东西.设置一次出生日期并从那里开始疯狂.

编辑:有几种方法可以从数据中创建对象.您可以预定义类并创建对象,也可以"动态"创建它们.

- >一些v简化示例 - 如果这还不够,我可以添加更多.

在飞行中:

$conn = DBConnection::_getSubjectsDB();  
$query = "select * from studies where Status = 1";  
$st = $conn->prepare( $query );  

$st->execute();  
$rows = $st->fetchAll();  
foreach ( $rows as $row )  
{  
    $study = (object)array();  
    $study->StudyId = $row[ 'StudyId' ];  
    $study->Name = $row[ 'StudyName' ];  
    $study->Investigator = $row[ 'Investigator' ];  
    $study->StartDate = $row[ 'StartDate' ];  
    $study->EndDate = $row[ 'EndDate' ];  
    $study->IRB = $row[ 'IRB' ];  

    array_push( $ret, $study );  
} 
Run Code Online (Sandbox Code Playgroud)

预定义:

/** Single location info
*/
class Location  
{  
    /** Name  
    * @var string  
    */  
    public $Name;  

    /** Address  
    * @var string  
    */  
    public $Address;  

    /** City  
    * @var string  
    */  
    public $City;

    /** State
    * @var string
    */
    public $State;

    /** Zip
    * @var string
    */
    public $Zip;

    /** getMailing
    * Get a 'mailing label' style output
    */
    function getMailing()
    {  
         return $Name . "\n" . $Address . "\n" . $City . "," . $State . "  " . $Zip;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

$conn = DBConnection::_getLocationsDB();  
$query = "select * from Locations where Status = 1";  
$st = $conn->prepare( $query );  

$st->execute();  
$rows = $st->fetchAll();  
foreach ( $rows as $row )  
{  
    $location = new Location();  
    $location->Name= $row[ 'Name' ];  
    $location->Address = $row[ 'Address ' ];  
    $location->City = $row[ 'City' ];  
    $location->State = $row[ 'State ' ];  
    $location->Zip = $row[ 'Zip ' ];  

    array_push( $ret, $location );  
} 
Run Code Online (Sandbox Code Playgroud)

然后你可以循环$ ret并输出邮件标签:

foreach( $ret as $location )
{ 
    echo $location->getMailing();
}
Run Code Online (Sandbox Code Playgroud)


kmo*_*y12 0

认为您正在谈论在两个或多个表上使用 SELECT 出现的奇怪对象。

我通过AS在我的 sql 中使用它来给它一个更简单的名称来解决这个问题。

SELECT IFNULL(SUM(table2.big_name),0) AS sumBig

...


$result=$PDO->fetchObject();
$sum=$result->sumBig;
Run Code Online (Sandbox Code Playgroud)