我正在尝试制作一个ORM,我有一个获取queryString的函数,但我遇到了错误
“注意:数组到字符串的转换”
我不知道该怎么解决。 如果您能帮助我,那就太好了,谢谢
public function selectOrderBy($columnName,$order){
$req = $this->getConnexion()->query('SELECT * FROM '.$this->getTable().' ORDER BY '.$columnName.' '.$order.'');
$req->execute();
$this->logRequest($req->queryString);
$results = $req->fetchAll();
return $results;
}
function logRequest($query){
$date = new DateTime();
$dateString = $date->format('Y-m-d H:i:s');
$filePath = "request.log";
$fp = fopen($filePath, "a+");
fputs($fp, "[".date('d/m/Y à H:i:s',time())."]" . $query ); //Error here
fclose($fp);
}
在request.log文件中,我已经知道了
[04/01/2019 à 10:18:05]SELECT * FROM animals ORDER BY id ASC
[04/01/2019 à 10:18:05]SELECT * FROM animals ORDER BY id ASC
[04/01/2019 à 10:18:05]Array
我在其他文件中使用它
$manager->logRequest($manager->selectOrderBy('id','ASC'));
答案 0 :(得分:1)
$query
可以将其作为数组变量,因此您可以使用:
public function selectOrderBy($columnName,$order){
$query='SELECT * FROM '.$this->getTable().' ORDER BY '.$columnName.' '.$order.'';
$req = $this->getConnexion()->query($query);
$req->execute();
$this->logRequest($req->queryString);
$results = $req->fetchAll();
$array['results']=$results;
$array['query']=$query;
return $array;
}
function logRequest($query){
$date = new DateTime();
$dateString = $date->format('Y-m-d H:i:s');
$filePath = "request.log";
$fp = fopen($filePath, "a+");
fputs($fp, "[".date('d/m/Y à H:i:s',time())."]" . $query ); //Error here
fclose($fp);
}
其他页面
$manager->logRequest($manager->selectOrderBy('id','ASC')['query']);
要在其他地方使用它,您只需将$manager->selectOrderBy('id','ASC')['query']
更改为$manager->selectOrderBy('id','ASC')['results']