0

I am building a PHP class to handle database management, and I wondered if it was possible to build a method which could receive a string with a concatenated array as one variable.

For example, take a look at this simplified code:

class Database {
    function __construct() {
        // Connect to the database, code not shown
    }

    public function query($input) {
        // Do something with the input so the string values are recognized ...
        // and the array and its keys are converted into an SQL string.
        // Code not shown...

        mysql_query($processedInput);
        return true;
    }
}

So, ideally, if I run something like this ...

$db = new Database();
$db->query("UPDATE `table` SET " . 
            array("id" = "2",
                  "position" = "1",
                  "visible" = "1",
                  "name" = "My Name's John",
                  "description" = "This is neat!"
            ) . " WHERE `id` = '1'");

... PHP would generate, then run this SQL ...

mysql_query("UPDATE `table` SET `id` = '2', `position` = '1', `visible` = '1', 
`name` = 'My Name\'s John', `description` = 'This is neat!' WHERE `id` = '1'");

I can do all of the nitty-gritty array conversion, but, for now, all I need is a way for PHP to break the input up into strings and arrays, then evaluate each one separately.

I would like to avoid passing multiple values into the method.

gobernador
  • 5,271
  • 3
  • 28
  • 50
Oliver Spryn
  • 15,563
  • 30
  • 91
  • 184
  • why are you trying to avoid passing multiple values into the method? this is at the root of your problem, and doesn't generally make much sense... – Igor Serebryany Mar 22 '11 at 16:55
  • I would like this method to be able to handle arrays and string in multiple locations, since, for example, an update SQL statement is totally different then an insert SQL statement. Doing it this way, I'm hoping that future development with this method will make things much easier, and simpler. – Oliver Spryn Mar 22 '11 at 16:57
  • My friend, the world does not need another database library. [Please consider using PDO](http://us2.php.net/manual/en/book.pdo.php) instead of writing your own. – Charles Mar 22 '11 at 17:02

2 Answers2

1

In Ruby you could do this, but you're out of luck in PHP. The good news is, you can modify what you're doing slightly to pass the query and the parameters separately as arguments to the query method:

$db->query("UPDATE `table` SET ? WHERE `id` = '1'", array(
  "id" = "2",
  "position" = "1",
  "visible" = "1",
  "name" = "My Name's John",
  "description" = "This is neat!"
);

And then handle the interpolation and concatenation in your Database object:

class Database {
    function __construct() {
        // Connect to the database, code not shown
    }

    public function query($query, $input) {
        $sql = $this->_normalize_query($query, $input)

        mysql_query($sql);
        return true;
    }

    protected function _normalize_query($query, $input) {
      $params = "";
      foreach($input as $k => $v) {
        // escape and assemble the input into SQL
      }
      return preg_replace('/\?/', $params, $query, 1);
    }
}

However

There are already a lot of ORMs out there that are very capable. If you are looking for something to only assemble queries and not manage results, you can probably find something as well. It seems like you're reinventing the wheel needlessly here.

Good PHP ORM Library?

Community
  • 1
  • 1
coreyward
  • 68,091
  • 16
  • 122
  • 142
  • Hmm... so to do it the way I was hoping, perhaps, the best way would be to use something like function_get_args(), the replace each "." with "," inside the method input? – Oliver Spryn Mar 22 '11 at 17:08
  • Thanks for your pointers, coreyward. They are much appreciated. – Oliver Spryn Mar 22 '11 at 17:23
1

You could write a sort of Helper functions which would work something like:

(inside of class Database { )

public function ArrayValues($array)
{
    $string = "";

    foreach($array as $Key => $Value)
    {
        $string .= "`$Key` = '$Value' ,";
    }
    // Get rid of the trailing ,

    // Prevent any weird problems
    if(strlen($string) > 1)
    {
        $string = substr($string, 0, strlen($string) - 2);
    }

    return $string;
}

Then you'd use it like:

$db->query("UPDATE `table` SET " . 
            $db->ArrayValues(array("id" = "2",
                  "position" = "1",
                  "visible" = "1",
                  "name" = "My Name's John",
                  "description" = "This is neat!"
            )) . " WHERE `id` = '1'");

I haven't tested this, however it should work.

Adam Casey
  • 1,560
  • 13
  • 20
  • Hmm... thanks @Dotmister, but adding the extra method in there would complicate something that was supposed to be made simpler. However... is it possible to pass a json array in there, then have PHP find and evaluate that? – Oliver Spryn Mar 22 '11 at 17:11
  • Well, yes you could pass a JSON array but surely that complicated it even more? – Adam Casey Mar 22 '11 at 17:15
  • For the average input it will receive, something like "UPDATE `table` SET {"id" : "2", "position" : "1", "visible" = "1", "name" : "My Name's John", "description" : "This is neat!"} WHERE `id` = '1'" _may_ not be that complicated. Well, I see I have two good options here. Thanks for your help! – Oliver Spryn Mar 22 '11 at 17:19