我使用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(.....???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(...) { ... }
答案 0 :(得分: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);