PHP连接数组和字符串的方法

发布于 2024-10-25 20:10:52 字数 1188 浏览 2 评论 0原文

我正在构建一个 PHP 类来处理数据库管理,我想知道是否可以构建一种可以接收带有串联数组的字符串作为一个变量的方法。

例如,看一下这个简化的代码:

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

因此,理想情况下,如果我运行这样的代码......

$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 将生成,然后运行此 SQL ...

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

我可以完成所有具体的数组转换,但是,现在,我需要的是 PHP 将输入分解为字符串和数组,然后分别评估每个字符串和数组的方法。

希望避免将多个值传递到该方法中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-11-01 20:10:53

在 Ruby 中你可以做到这一点,但在 PHP 中你就不那么幸运了。好消息是,您可以稍微修改您正在做的事情,将查询和参数分别作为参数传递给查询方法:

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

然后处理数据库对象中的插值和串联:

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

但是

已经有很多 ORM 了那里有非常有能力的人。如果您正在寻找组合查询而不管理结果的东西,您可能也可以找到一些东西。看来你在这里不必要地重新发明轮子。

好的 PHP ORM 库吗?

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?

小…红帽 2024-11-01 20:10:53

您可以编写一种辅助函数,其工作方式如下:(

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

然后您可以像这样使用它:

$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'");

我还没有测试过它,但它应该可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文