0

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

`

  • What exactly is the question? $connection->query($sql) <<< $connection is undefined. You are wide open to sql injection. – baao Jul 26 '15 at 01:14
  • $connection is the conn. sorry for the error. – user3369804 Jul 26 '15 at 01:35
  • 1
    possible duplicate of [Object oriented php class simple example](http://stackoverflow.com/questions/20603992/object-oriented-php-class-simple-example) – Heroselohim Jul 26 '15 at 01:35
  • the problem I have is the returned values are not the same as in my function. in function i return the message and true. in my method return an object and nothing – user3369804 Jul 26 '15 at 01:36

1 Answers1

0

The way you call DBA_Insert method shows that you don't assign returned value. $insertMsg is just object instance (variable that holds a pointer/link) of dba class and you call one of its methods. With proper names your code should like this:

$dba = new dba();
$result = $dba->insert(...);

I've changed method name as well, because it will always belong to objects of dba class and your instance variable name should also be self explanatory.

shudder
  • 2,076
  • 2
  • 20
  • 21
  • _Thanks for your response_. resolve my problem, and thanks for the explanation of my problem, and the reason for rename the method.. **many thanks** – user3369804 Jul 26 '15 at 22:44