简体   繁体   English

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

[英]How can I pass the mySQL Result in Class to an external function?

I use a framework for MySQL queries.我使用 MySQL 查询的框架。 In the framework, I would like to call a separate function for tracking and logging.在框架中,我想调用一个单独的 function 进行跟踪和记录。 How can I pass the result array of MySQL Query to this function?如何将 MySQL 查询的结果数组传递给这个 function?

/* 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(.....???HOW CAN I PASS THE RESULT FROM MYSQL QUERY TO THESE 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(...) { ... }

In our DB layer we use __destruct to close all connections and write log file like so:在我们的 DB 层中,我们使用 __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();
        }
    }
}

or using custom parameter或使用自定义参数

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

in your method update the parameter在您的方法中更新参数

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;

} }

and pass it to your function like so - outside the class并将其传递给您的 function 像这样 - 在 class 之外

$regular_result = $db->query($query)->fetchAll();   
my_own_tracking_function($db->DataTracking);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM