如何将Class中的mySQL结果传递给外部函数?

问题描述 投票:0回答:1

我使用一个用于MySQL查询的框架。在框架中,我想调用一个单独的函数来进行跟踪和记录。如何将MySQL Query的结果数组传递给此函数?

/* MySQL class: */
public function query($query, $map = [], $cleanQuery = '')  {           
    $raw = $this->raw($query, $map);
    $query = $this->buildRaw($raw, $map);
    return $this->exec($query, $map, $cleanQuery);
}

public function exec($query, $map = [], $cleanQuery = '')   {
    $this->logs = [[$query, $map]];     
    $statement = $this->pdo->prepare($query);

    if ($statement)     {
        foreach ($map as $key => $value)    {
            $statement->bindValue($key, $value[ 0 ], $value[ 1 ]);
        }

        $statement->execute();
        $affectedRows = $statement->rowCount();
        $errorinfos = $statement->errorInfo();

        my_own_tracking_function(   $this->generate($query, $map), 
                                    $affectedRows, $errorinfos, 
                                    .....HOW CAN I PASS THE RESULT TO MY FUNCTION?....);

        $this->statement = $statement;
        return $statement;
    }       
    return false;
}

/* Regular MySQL query in my code: */
$regular_result = $db->query($query)->fetchAll();

/* my own tracking function */
function my_own_tracking_function(...) { ... }
php mysql medoo
1个回答
0
投票

在我们的数据库层,我们使用__destruct关闭所有连接并像这样写日志文件:

public function __destruct() {
    // in debug mode write log file
    if (self::$DebugMode) self::_write_log_file();
    // close all active connections
    foreach (self::$CONNECTIONS as $connection) {
        if (is_object($connection)) {
            $connection->close();
        }
    }
}

或使用自定义参数

    //add to top of your class
public $DataTracking = [];

在您的方法中更新参数

public function exec($query, $map = [], $cleanQuery = '')   {
$this->logs = [[$query, $map]];     
$statement = $this->pdo->prepare($query);

if ($statement)     {
    foreach ($map as $key => $value)    {
        $statement->bindValue($key, $value[ 0 ], $value[ 1 ]);
    }

    $statement->execute();
    $affectedRows = $statement->rowCount();
    $errorinfos = $statement->errorInfo();

    $this->DataTracking[] = 'Anything you need to log';

    $this->statement = $statement;
    return $statement;
}       
return false;

}

并像这样将其传递给您的函数-在类之外

$regular_result = $db->query($query)->fetchAll();   
my_own_tracking_function($db->DataTracking);
© www.soinside.com 2019 - 2024. All rights reserved.