I have a small problem returning value of a php method. in a function works like i want but when i try to create a class and the proper methods i can return the value correct.
if i use this function and call it the result is what i want:
function InsertData($table, array $data){
$connection = DataBaseConnection();
$row = implode(", ", array_keys($data));
$column = "'".implode("', '", array_values($data))."' ";
$sql = "INSERT INTO {$table} ( $row ) VALUES ( $column )";
if($connection->query($sql) === FALSE){
return "Error: " . $connection->error; // return the error
}else{
return True; // return true
}}
i call the function this way, and works fine. retrun true if the data is insert and "ERROR + error msg" if exists an error.
$result = InsertData( 'table', $data_array);
but i want to put every stuf of the data base in a class, and the class have the proper methods. i am a new developer in php oop, and i have a problem. I can return the same values of my previous function in my correspondent method of the class
class dba{
public function conn{
/*some code*/ return $conn; //
}
public function DBA_Insert($table, array $data) {
$conn = $this->Conn() ;
$row = implode(", ", array_keys($data));
$column = "'".implode("', '", array_values($data))."' ";
$sql = "INSERT INTO {$table} ( $row ) VALUES ( $column )";
if($conn->query($sql) === FALSE){
return "Error: " . $conn->error;
// this return an object and i want to retrun the string ERROR + Error msg like in my function
}else{
return true;
// don't return nothing but the data is insert in the data base, and i wan´t to return true to use in the other side of the aplication
}
$conn->close();
}
to call the class and the method this way:
$insertMsg = new dba();
$insertMsg->DBA_Insert( 'messages', $data);
if you can help me i apreciate. thanks for your time ;)
`