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

  • Home
  • SEARCH
  • 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 542199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:25:18+00:00 2026-05-13T10:25:18+00:00

I am trying to save lots of variables to a database and it is

  • 0

I am trying to save lots of variables to a database and it is getting ridiculous now. I am using PHP and MySQL.

Is there a way, I can get the array value and the array keys (array keys are exactly the same as the table column/field names) in one go without having to add a new variable and table column pair.

To be honest, I just need help with constructing the SQL String only, the rest is setup for me.

I can add the new column to the database table when I have to store a new variable.

Thanks all for any help

  • 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-13T10:25:19+00:00Added an answer on May 13, 2026 at 10:25 am

    If you want to create a SQL query from your array, this might help:

    // Sample array
    $array = array(
                 'key1' => 'value1',
                 'key2' => 'value2'
                 ...
                 'key10' => 'value10'
             );
    
    // Get and escape the keys
    $keys = array_map('mysql_real_escape_string', array_keys($array));
    // Escape the values
    $array = array_map('mysql_real_escape_string', $array);
    // Build query
    $query = "INSERT INTO table(`".implode('`, `', $keys)."`) VALUES('".implode("', '", $array)."')";
    
    mysql_query($query);
    

    In this case, the query would look something like this:

    INSERT INTO
        table(`key1`, `key2` ... `key10`)
    VALUES
        ('value1', 'value2' ... 'value10')
    

    If you have a multidimensional array (an array of arrays) you can create a query as follows:

    // Sample multidimensional array
    $array = array(
                 array('key1' => 'value1', 'key2' => 'value2'),
                 array('key1' => 'value3', 'key2' => 'value4'),
                 array('key1' => 'value5', 'key2' => 'value6')
             );
    
    // Get and escape the keys
    $keys = array_map('mysql_real_escape_string', array_keys(current($array)));
    // Array to store values for the query
    $values = array();
    // Loop every row and insert into $values array
    foreach($array as $row) {
        // Escape all items
        array_map('mysql_real_escape_string', $row);
        $values[] = "('".implode("', '", $row)."')";
    }
    
    $query = "INSERT INTO table(`".implode('`, `', $keys)."`) VALUES ".implode(', ', $values);
    
    mysql_query($query);
    

    And in this case, the resulting query would be something like this:

    INSERT INTO
        table(`key1`, `key2`)
    VALUES
        ('value1', 'value2'),
        ('value3', 'value4'),
        ('value5', 'value6')
    

    Now only thing you have to worry about is creating the corresponding columns to the database.

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

Sidebar

Related Questions

When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save data with following structure: As you can see, there is
I'm trying to save a CUploadedFile object in local folder, I am using img
I am trying to save a simple template to pdf using the rendering plugin,
I am trying to save multiple image using this code. But instead its saving
I am trying to save a time in a MySQL table. However, the date
I am reading text files (RDF) using the NxParser library. I am getting lots
When trying to save the parent with a many-to-one relationship in the database the
I am trying to make a app which can save the web page offline
Great people of StackOverflow, save my ass please :) I've been trying to get

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.