1

I'm trying to make an ORM, I have a function to get a queryString but I got an error

"Notice: Array to string conversion"

I don't know how to solve it. If you can help me, it's could be very nice, thanks

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);
}

On file request.log I've got this

[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

And I use this in other file

$manager->logRequest($manager->selectOrderBy('id','ASC'));
6
  • Please ALWAY show ALL the error message, not a summary. Also identify the line in the code that is mentioned on the error message Commented Jan 4, 2019 at 10:13
  • I'm sorry, this error is for this line -> fputs($fp, "[".date('d/m/Y à H:i:s',time())."]" . $query ); Commented Jan 4, 2019 at 10:14
  • Can you dump $query Commented Jan 4, 2019 at 10:15
  • Small Point the date() function does not require the time() parameter unless that is not the current time Commented Jan 4, 2019 at 10:15
  • fyi, if this is MySQL, the general_log will give you exactly what you want. Dont roll your own. Commented Jan 4, 2019 at 10:19

1 Answer 1

1

Its sames you $query var its an array so you can just use :

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);
}

Other page

$manager->logRequest($manager->selectOrderBy('id','ASC')['query']);

to use it in onother place you can just change $manager->selectOrderBy('id','ASC')['query'] with $manager->selectOrderBy('id','ASC')['results']

Sign up to request clarification or add additional context in comments.

9 Comments

I have this Warning: implode(): Invalid arguments passed in the same line I've got this error
Warning: implode(): Invalid arguments passed in C:\wamp64\www\myORM\myORM.php on line 83
Notice: Array to string conversion in C:\wamp64\www\myORM\myORM.php on line 83
@askemeline is $query equal to return selectOrderBy(..)
ok now i undertood do you want it like that SELECT * FROM animals ORDER BY id ASC or you want values ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.