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 8307395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:31:00+00:00 2026-06-08T18:31:00+00:00

I’m using MySQLi for a project (but could easily switch to PDO, read the

  • 0

I’m using MySQLi for a project (but could easily switch to PDO, read the edit at the bottom). What I want is to be able to process an associative array into an mysql query without typing every key/value pair manually. I know that might be simply put, but I’m still in a learning process when it comes to MySQLi/PDO. To make it clear what I mean here is an example:

Let’s say I have this associative array:

$data = array(
    'name' => 'Objective',
    'short_name' => 'OB',
    'generation' => 1
);

What I want is to create a query like this:

UPDATE signals SET ? = ? WHERE sid = '1'

And it to become a query like this (or instead of an UPDATE, an INSERT, SELECT etc.):

UPDATE signals SET
    name = 'Objective', 
    short_name = 'OB', 
    generation = 1 
WHERE sid = '1'

So basically my question is: is this possible with MySQLi or PDO itself? If it’s possible how would I have to do this?
I’ve read on preparing statements before executing, but it’s not getting to me yet. Hopefully someone can help me out.

Edit: I’m still very early into the project so i’m also fine with using PDO, but the same question remains. I did look into both PDO and mysqli, and I’m not sure how to do it with PDO either. So for the sake of the question i’ll add PDO to as a tag and to the question.

  • 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-06-08T18:31:00+00:00Added an answer on June 8, 2026 at 6:31 pm

    Here is a function that will take an input array and produce something you can drop straight into your query, as long as you are only updating a single table:

    function array_to_pdo_params($array) {
      $temp = array();
      foreach (array_keys($array) as $name) {
        $temp[] = "`$name` = ?";
      }
      return implode(', ', $temp);
    }
    

    Now you can perform your PDO query like this:

    // The data
    $data = array(
      'name' => 'Objective',
      'short_name' => 'OB',
      'generation' => 1
    );
    
    // The parameter for the WHERE clause
    $sid = 1;
    
    // Build the query string
    $query = "
      UPDATE `signals`
      SET ".array_to_pdo_params($data)."
      WHERE `sid` = ?
    ";
    
    // Convert the data array to indexed and append the WHERE parameter(s) to it
    $queryData = array_values($data);
    $queryData[] = $sid;
    
    $stmt = $db->prepare($query); // Obviously add the appropriate error handling
    
    $stmt->execute($queryData);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.