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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:18:43+00:00 2026-06-10T09:18:43+00:00

I need to insert array data into MySQL DB. My code is provided below.

  • 0

I need to insert array data into MySQL DB. My code is provided below. The problem is that query is equal to

INSERT INTO MyTab (Array) VALUES
(Array,Array,Array,Array,Array,Array,Array,Array,Array,Array,Array)

So, why do I get Array instead of array values?

$columns = array();
    $values = array();

    $columns[] = array('Num','appearanceTime');

    $curr_time = new DateTime();
    while($row=mysql_fetch_assoc($result_arr)) {
        $values[] = array($row['Num_arr'],$curr_time);
    }

    $cols = implode(",",$columns);
    $vals = implode(",",$values);

$query = "INSERT INTO `MyTab` ($cols) VALUES ($vals)";

UPDATE
This code returns Internal Server Error at the line $vals = implode(...).

$columns = array(‘Num’,’appearanceTime’,’earliestTime’);
$values = array();

$curr_time = new DateTime();
while($row=mysql_fetch_assoc($result_arr)) {
    $values[] = array($row['Num_arr'],$curr_time,$row['ETA']);
}

$cols = implode(",",$columns);

function get_values($arr) {
return '(' . implode(',', $arr) . ')';
}

$vals = implode(',', array_map('get_values', $values));

$query_queue = "INSERT INTO `MyTab` ('" . $cols . "') VALUES ('" . $vals . "')";
  • 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-10T09:18:44+00:00Added an answer on June 10, 2026 at 9:18 am

    The values inside the arrays are arrays. You need to implode each of them, too:

    $vals = implode(',', array_map(function($arr) {
        return '(' . implode(',', $arr) . ')';
    }, $values));
    

    As for the columns, I think you want:

    $columns = array('Num','appearanceTime');
    $values = array();
    

    Not:

    $columns = array();
    $values = array();
    
    $columns[] = array('Num','appearanceTime');
    

    You’ll also need to quote everything to put it in the query. You should use PDO or MySQLi and prepared statements instead of mysql_ if you can.


    Given PHP 5.2, the first example needs to be changed to:

    function implode_comma($arr) {
        return '(' . implode(',', $arr) . ')';
    }
    
    # ...
    
    $vals = implode(',', array_map('implode_comma', $values));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help with PHP Mysql code to insert multiple select data into database. With
I'm trying to insert form data into a MySQL 4.1 DB. The problem I'm
I need to know how I can insert data inside objects inside an array
I need to insert a huge amount of data into different tables within my
I have an array with more than 134675+ values, I need to insert them
Here's the thing, I don't have access to code that inserts data into a
I am facing the problem to insert the data into dictionary. It is not
i need help to parsing json data to mysql. my code its not work,
I need to retrieve data from several rows and then insert the results into
I'm trying to figure out how to insert/update data into offsite databases that don't

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.