I'am trying to replace a part of a string for another value and i'am doing this for some logs that i need to create, to have the full query. I'am using PDO.
I want to in the end have the correct query to print on the log.
public function queryBindValues($string,$data,$queriesLog) {
echo gettype($data); //object
$toArray = (array)$data; //convert to array
$finalArray = [];
//remove class name(on key) from array
foreach($toArray as $k=>$v) {
$k = str_replace(get_class($data), "", $k);
$finalArray[$k] = $v;
}
/*
$finalArray
Array
(
[id] =>
[gender] => vbn
[status] => 1
[user_id] => TLDICA
[last_update] => 2018-04-25
)
*/
//string = INSERT INTO type_genders (status, user_id, last_update, gender) VALUES (:status, :user_id, :last_update, :gender);
foreach($finalArray as $k=>$v) {
$string = str_replace(":$k", $v, $string);
}
echo $string;
//INSERT INTO type_genders (status, user_id, last_update, gender) VALUES (:status, :user_id, :last_update, :gender)
}
Can someone help me? Thanks,