Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4082140
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:09:01+00:00 2026-05-20T18:09:01+00:00

I am building a PHP class to handle database management, and I wondered if

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:09:02+00:00Added an answer on May 20, 2026 at 6:09 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When building XML in PHP, is it quicker to build a string, then echo
I'm building up a library of filters for a validation class in PHP, some
When building some of my PHP apps, a lot of the functionality could be
I'm building a class or two based on StdRegProv to handle Windows Registry via
I'm currently building a class in PHP that generates PDF documents using the WKHTMLTOPDF
Im currently building a php framework... again. I have a class called config. its
I need to build a PHP photo processing class, I know there are MANY
I'm building a simple PHP class that will allows you to enter your twitter
If I create a new PHP class e.g. to simplify form building (yes I
Lets say I'm building a base class which will be extended upon by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.