I need to insert a Javascript object into a new MySQL table using PHP. My object is in the following form:
{
rowA:[A1,A2,A3],
rowB:[B1,B2,B3],
rowC:[C1,C2,C3],
}
I also have an array of column names:
$columns = array("col1","col2","col3");
I need to use this data to create and fill a MySQL table in the following format:
col1 col2 col3
rowA A1 A2 A3
rowB B1 B2 B3
rowC C1 C2 C3
I can access my server and create a table, but am still unsure how to deal with JSON:
$str = file_get_contents('barchartData/US-HI.json');
$json = json_decode($str, true);
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "CREATE TABLE testTable (
// not sure how to specify column, row, and cell values from the JSON...
)";
I am fairly new to PHP and haven't been able to get code in PHP working which read the JSON keys and set them as MySQL row values.
var_dump(json_decode($json));What do you think you can add in the database and how?