0

Here's what i'm trying to achieve: i'm learning OOP for school and i have tp make a registration thingy.... it's working but a lot is hardcoded and i'd like to make an universal insert function which works like $class->insert "1, 2, 3", "foo, bar, thing" where it'll insert foo into 1 bar into 2 and thing into 3 here comes the problem: i don't know how to convert my PHP array into SQL variables because i do not know how many items there are in my PHP array. (due to an explode)

here's my code so far:

<?php

 class database
{
static protected $_connection;

public function __construct($host, $dbname, $username, $password)
{
    self::$_connection = new PDO("mysql:host=" . $host . ";dbname=" . $dbname, $username, $password);
    self::$_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return self::$_connection;

}

public function insert($tablename, $columns, $values)
{
    $exploded = explode(", ", $values);
    $valuenumber = NULL;
    $string = NULL;
    $teller = 0;


    $query = self::$_connection->prepare('INSERT INTO gebruikers (:naam) VALUES ');
    foreach ($exploded as $array) {
        $query->bindparam(":value" . $valuenumber, $array);
        $valuenumber++;
    }
    for ($i = $valuenumber; $i > 0; $i--) {
        $string .= ", :value" . $teller;
        $teller++;
        // alles wordt in een string gezet: ":value, :value1, :value2
    }
    $stringarray = explode($string, ", ");
    $query->execute(array(
        'naam' => $teller
    ));
}

}



$client = new database("localhost", "oop", "root", "");
$client->insert("gebruikers", "naam", "test");

I'm sorry if it's a bit messy and for the lack of comments i'm pretty new in this.

Could anyone please help me out a bit? thanks,

g3.

5
  • please stop using singletons for DB connection Commented Apr 3, 2014 at 18:26
  • singleton = referring to the self::? according to my teacher when something's static you need to use self:: Commented Apr 3, 2014 at 20:36
  • My point is that you do not need to use global scope to have a shared DB connection between multiple objects. This might give you some hints: stackoverflow.com/a/11369679/727208 Commented Apr 3, 2014 at 20:37
  • thanks, i'll check it out, the reason i use a static is because i thought that was the appropriate way of making classes share a connection rather than every class creating it's own instance of the connection Commented Apr 3, 2014 at 20:39
  • well ... you are partially right: each class should not make its own connection, but using global state is the wrong way to go about it. Commented Apr 4, 2014 at 6:49

1 Answer 1

1
public function insert($table, $columns, $values)
{
    // make sure we have the same amount of keys and values
    if(count($columns) !== count($values)){
        return false;
    }

    $query = self::$_connection->prepare('INSERT INTO ' . $table . ' (' . implode(',', $columns) . ') VALUES (:' . implode(',:', $columns) . ')');

    $index = 0;
    foreach($columns as $column){
        $query->bindparam(":" . $column . $values[$index++]);
    }

}

If I were you, I'd get rid of passing $columns and $values separately and just pass a key => pair array like:

array('id' => 1, 'name' => 'new name');

I understand this is a learning process for you're sort of re-inventing the wheel here. Check out libraries like doctrine.

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

2 Comments

thanks for the answer, i'll try it out ^.^ + i'm not allowed to use any exentions yet but we're going to use Laravel next week
That lowercase "p" in bindparam threw me right off. That almost looked like it was a missing underscore; alas it's PDO. I always use an uppercase "P" for clarity myself ;-)

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.