简体   繁体   中英

Generating MySQL table and columns automatically from JSON data

I am trying to read JSON I have retrieved from an API. I want to take that data and automatically create the table and columns needed to store the received information. I don't want to statically make the columns because each coin I run this own may have more or less information.

My code below currently will only create 3 columns (id, name, and tickers) and insert data into id and name.
It will not make it past the first array of information.
Secondly, I get an error at '$qi.= "'". mysqli_real_escape_string($conn, $value). "',";' '$qi.= "'". mysqli_real_escape_string($conn, $value). "',";' Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given which I do not know how to fix.

Original Code

$apiurl = "https://api.coingecko.com/api/v3/coins/infocoin/tickers";
$json = file_get_contents($apiurl);

    JSON_to_table($json);

            function JSON_to_table($json, $tblName = "New_JSON_table_"){
    $conn = mysqli_connect($GLOBALS["db"]["host"], $GLOBALS["db"]["user"], $GLOBALS["db"]["pass"], $GLOBALS["db"]["name"]);
            $j_obj = json_decode($json, true);
        //$j_obj2 = $j_obj["tickers"];
        //var_dump($j_obj);
    print_r ($j_obj);
            if(!mysqli_num_rows( mysqli_query($conn,"SHOW TABLES LIKE '" . $tblName . "'"))){ 
                $cq = "CREATE TABLE ". $tblName ." (
                id1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,";
                foreach($j_obj as $j_arr_key => $value){
                    $cq .= $j_arr_key . " VARCHAR(256),";

        }
                $cq = substr_replace($cq,"",-1);
                $cq .= ")";
                mysqli_query($conn,$cq) or die(mysqli_error($conn));
            }

            $qi = "REPLACE INTO $tblName (";
            reset($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= $j_arr_key . ",";
                }
                $qi = substr_replace($qi,"",-1);
            $qi .= ") VALUES (";
            next($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= "'" . mysqli_real_escape_string($conn, $value) . "',";
            $qi .= "'" .$value . "',";
                }
            $qi = substr_replace($qi,"",-1);
            $qi .= ")";
            $result = mysqli_query($conn,$qi) or die(mysqli_error($conn));

        return true;

Code that gives duplicate column name error

$apiurl = "https://api.coingecko.com/api/v3/coins/infocoin/tickers";
$json = file_get_contents($apiurl);

    JSON_to_table($json);

            function JSON_to_table($json, $tblName = "New_JSON_table_"){
    $conn = mysqli_connect($GLOBALS["db"]["host"], $GLOBALS["db"]["user"], $GLOBALS["db"]["pass"], $GLOBALS["db"]["name"]);
            $j_obj = json_decode($json, true);
        //$j_obj2 = $j_obj["tickers"];
        //var_dump($j_obj);
    print_r ($j_obj);
            if(!mysqli_num_rows( mysqli_query($conn,"SHOW TABLES LIKE '" . $tblName . "'"))){ 
                $cq = "CREATE TABLE ". $tblName ." (
                id1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,";
                foreach($j_obj as $j_arr_key => $value){
                    $cq .= $j_arr_key . " VARCHAR(256),";

        }
        next($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $cq .= $j_arr_key . " VARCHAR(256),";

        }
                $cq = substr_replace($cq,"",-1);
                $cq .= ")";
                mysqli_query($conn,$cq) or die(mysqli_error($conn));
            }

            $qi = "REPLACE INTO $tblName (";
            reset($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= $j_arr_key . ",";
                }
                $qi = substr_replace($qi,"",-1);
            $qi .= ") VALUES (";
            next($j_obj);
                foreach($j_obj as $j_arr_key => $value){
                    $qi .= "'" . mysqli_real_escape_string($conn, $value) . "',";
            $qi .= "'" .$value . "',";
                }
            $qi = substr_replace($qi,"",-1);
            $qi .= ")";
            $result = mysqli_query($conn,$qi) or die(mysqli_error($conn));

        return true;

I have adjusted the code in multiple ways. I have finally made it insert all the data from the second array into the 'tickers' column but I don't want that. I have gotten the code to almost add the remaining column names but it then gives me an error Duplicate column name 'name'

Edited Code From Comments Below

    $apiurl = "https://api.coingecko.com/api/v3/coins/infocoin/tickers";
    $json = file_get_contents($apiurl);

        JSON_to_table($json);

                function JSON_to_table($json, $tblName = "New_JSON_table_"){
        $conn = mysqli_connect($GLOBALS["db"]["host"], $GLOBALS["db"]["user"], $GLOBALS["db"]["pass"], $GLOBALS["db"]["name"]);
                $j_obj = json_decode($json, true);
            //$j_obj2 = $j_obj["tickers"];
            //var_dump($j_obj);
        print_r ($j_obj);
                if(!mysqli_num_rows( mysqli_query($conn,"SHOW TABLES LIKE '" . $tblName . "'"))){ 
                    $cq = "CREATE TABLE ". $tblName ." (
                    id1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,";
                    foreach($j_obj["tickers"][0] as $j_arr_key => $value){
                        $cq .= $j_arr_key . " VARCHAR(256),";

            }
                    $cq = substr_replace($cq,"",-1);
                    $cq .= ")";
                    mysqli_query($conn,$cq) or die(mysqli_error($conn));
                }

                $qi = "REPLACE INTO $tblName (";
                reset($j_obj["tickers"][0]);
                    foreach($j_obj["tickers"][0] as $j_arr_key => $value){
                        $qi .= $j_arr_key . ",";
                    }
                    $qi = substr_replace($qi,"",-1);
                $qi .= ") VALUES (";
                next($j_obj);
                    foreach($j_obj["tickers"][0] as $j_arr_key => $value){
                        $qi .= "'" . mysqli_real_escape_string($conn, $value) . "',";
                $qi .= "'" .$value . "',";
                    }
                $qi = substr_replace($qi,"",-1);
                $qi .= ")";
                $result = mysqli_query($conn,$qi) or die(mysqli_error($conn));

            return true;

Errors From Updated Code

        Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /var/www/html/exp/coingecko_testmarket.php on line 86

        Notice: Array to string conversion in /var/www/html/exp/coingecko_testmarket.php on line 87

         Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /var/www/html/exp/coingecko_testmarket.php on line 86

        Notice: Array to string conversion in /var/www/html/exp/coingecko_testmarket.php on line 87

        Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in /var/www/html/exp/coingecko_testmarket.php on line 86

        Notice: Array to string conversion in /var/www/html/exp/coingecko_testmarket.php on line 87
       Column count doesn't match value count at row 1

Problem is, that data in this json is stored as multi-level structure (objects that contains another objects) that you can't store it in SQL database as it is.

You should generate 'create table' query for first item from 'tickers array $j_obj['tickers'][0] and then create 'insert' query for each element of that array, but you still have values that are not simple strings so you need to convert it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM