I am using an API that returns search results as json. Then I need to write this to the MYSQL table. I did this successfully before, but this time the situation is different, and I think that it is due to the structure of the resulting array: the key names are dynamic in this case, if there is no data for a specific key, the key is not listed in the array. The following is an example of a vardump 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)
Please note that the order / inclusion of keys is subject to change.
My suggested solution was to use array keys as column names, turning them into the variable used in the query expression, but this does not work for me. Here is 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? Thanks!
UPDATE 11/27:
Here I am trying to adapt David's suggestion. I get the following error: "Error connecting to database (1110)" Header name "specified twice in the request.
Here is my code in its current form:
$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 - WORK!
He works. Here is what we have:
$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;