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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:31+00:00 2026-06-14T15:30:31+00:00

I am using an API that returns search results as json. Then, I need

  • 0

I am using an API that returns search results as json. Then, I need to write this to a MYSQL table. I’ve done this successfully before, but this time the situation is different, and I think that’s because of the structure of the resulting array: the key names are dynamic in that, if no data exists for a particular key, the key is not listed in the array. Here is an example vardump of the array:

array
  0 => 
    array
      'title' => string 'Funny but not funny' (length=19)
      'body' => string 'by Daniel Doi-yesterday while eating at Curry House...
      'url' => string 'http://danieldoi.com/2012/11/20/funny-but-not-funny/' 
      'source_site_name' => string 'WordPress.com' (length=13)
      'source_site_url' => string 'http://www.wordpress.com' (length=24)
      'query_topic' => string 'thanksgiving' (length=12)
      'query_string' => string 'blogs=on&topic=thanksgiving&output=json' (length=39)
  1 => 
    array
      'title' => string 'Travel Easy this Holiday Season...' (length=34)
      'body' => string 'Give yourself a few gifts and get this holiday season off...
      'url' => string 'http://facadebeauty.wordpress.com/2012/11/20
      'date_published' => string 'Tue, 20 Nov 2012 18:22:35 +0000' (length=31)
      'date_published_stamp' => string '1353435755' (length=10)

Notice how the order/inclusion of the keys is subject to change.

My proposed solution was to use the array keys as column names, turning them into a variable to use in a query statement, but this isn’t working for me. Here’s my attempt:

$jsonString = file_get_contents("http://search-query-URL&output=json");
$array = json_decode($jsonString, true);

// database connection code snipped out here

$table = "results";

foreach($array as $arr_value) {
        foreach ($arr_value as $value) {
          $colName = key($arr_value);
          $colValue = ($value);
          $insert="INSERT INTO $table ($colName) VALUES ('$colValue')";

          mysql_query($insert) OR die(mysql_error());
          next($arr_value);
              }
         }

Any suggestions on where to look next? Thank you!

UPDATE 11/27:

Here I am trying to adapt David’s suggestion. I’m receiving the following error: “Database Connection Error (1110) Column ‘title’ specified twice on query.”

Here’s my code as it stands now:

$mysqli = mysqli_connect("localhost");
mysqli_select_db($mysqli, "mydatabase");

foreach ($array as $column) {
    foreach ($column as $key => $value) {
    $cols[] = $key;
    $vals[] = mysqli_real_escape_string($mysqli, $value);
    }
}

$colnames = "`".implode("`, `", $cols)."`";
$colvals = "'".implode("', '", $vals)."'";
$mysql = mysqli_query($mysqli, "INSERT INTO $table ($colnames) VALUES ($colvals)") or die('Database Connection Error ('.mysqli_errno($mysqli).') '.mysqli_error($mysqli). " on query: INSERT INTO $table ($colnames) VALUES ($colvals)");
mysqli_close($mysqli);
if ($mysql)
return TRUE;
else return FALSE;

Final Upate – WORKING!

It’s working. Here’s what we’ve got:

$mysqli = mysqli_connect("localhost");
mysqli_select_db($mysqli, "mydatabase");
  foreach ($array as $column) {
foreach ($column as $key => $value) {
    $cols[] = $key;
    $vals[] = mysqli_real_escape_string($mysqli, $value);
    }
  $colnames = "`".implode("`, `", $cols)."`";
  $colvals = "'".implode("', '", $vals)."'";
  $mysql = mysqli_query($mysqli, "INSERT INTO $table ($colnames) VALUES ($colvals)") or die('Database Connection Error ('.mysqli_errno($mysqli).') '.mysqli_error($mysqli). " on query: INSERT INTO $table ($colnames) VALUES ($colvals)");

  unset($cols, $vals);
}
  mysqli_close($mysqli);
if ($mysql)
return TRUE;
else return FALSE;  
  • 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-14T15:30:32+00:00Added an answer on June 14, 2026 at 3:30 pm

    I actually have just this function sitting around because I use it all the time. What this does: pool all the associative pairs in your array for insertion into the table, and puts them in as a single insert. To be clear: this isn’t what you’re doing above, as it looks like you’re trying to insert each value in its own insert, which would probably give you way more rows than you want.

    function mysqli_insert($table, $assoc) {
        $mysqli = mysqli_connect(PUT YOUR DB CREDENTIALS HERE);
        mysqli_select_db($mysqli, DATABASE NAME HERE);
        foreach ($assoc as $column => $value) {
            $cols[] = $column;
            $vals[] = mysqli_real_escape_string($mysqli, $value);
        }
        $colnames = "`".implode("`, `", $cols)."`";
        $colvals = "'".implode("', '", $vals)."'";
        $mysql = mysqli_query($mysqli, "INSERT INTO $table ($colnames) VALUES ($colvals)") or die('Database Connection Error ('.mysqli_errno($mysqli).') '.mysqli_error($mysqli). " on query: INSERT INTO $table ($colnames) VALUES ($colvals)");
        mysqli_close($mysqli);
        if ($mysql)
            return TRUE;
        else return FALSE;
    }
    

    As MarcB says above, there is a risk that your target table won’t have a column that your results show. But we are sanitizing the insertion (with mysqli_real_escape_string) so we shouldn’t have issues with injections vulnerabilities.

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

Sidebar

Related Questions

I am calling an API that returns JSON data. I am using json.loads to
I have this 'web service' I made that when accessed: http://192.168.0.1/api/v1/search/?query=thequery returns a JSON
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
Situation: I need to make an imap client (using java mail api) that if,
I'm developing a API that uses lambda expressions to specify properties. I'm using this
I understand that using the facebook API I need an api key to connect,
I'm using Google's Custom Search API to dynamically provide web search results. I very
I have this code that will return keyword suggestion from yahoo using the api.
I m using FALCON semantic search engine RESTful API & wrote this program But
I have a search Api I'm working on that needs to return search results

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.