Jump to content

PDO double entries


ludo1960

Recommended Posts

Hmmm, the only time i've seen odd characters floating about, was when I copied and pasted from this site. Here's the code again. i've triec mousepad and open office but nothing shows up, even tried https://marketplace.visualstudio.com/items?itemName=ShaneRay.InvisibleCharacterVisualizer#qna still no superfluous characters 

$db = pdoConnect();

$db->query("DROP TABLE IF EXISTS test2DB");

$db->query("CREATE TABLE test2DB (

    ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    Page_id INT,
    County VARCHAR(50) NOT NULL,
    Country VARCHAR(50) NOT NULL,
    Post_Town VARCHAR(50) NOT NULL

)");

 

Link to comment
Share on other sites

For anybody following on, here is the finished working code. If you're faced with a crazy multi dimensional array from a JSON source that you have to get into a mysql database, this is for you!

 

<?php    

function FlattenMultiArray($array,$bKeepKeys=true,$key_prefix='')
{
    
    $array_flattened=Array();
    foreach($array as $key=>$value){

        if(Is_Array($value)){
            $array_flattened=Array_Merge(
                $array_flattened,
                FlattenMultiArray($value,$bKeepKeys,$key)
            );
        }
        else{
            if($bKeepKeys){
                $array_flattened["{$key_prefix}{$key}"]=$value;
            }
            else{
                $array_flattened[]=$value;
            }
        }
    }

    return $array_flattened;
}

include 'connection.php' ;

$startnum = 41;
$endnum = 45;

//$placeholders = []; //not required!
$values = [];

for($i = $startnum; $i <= $endnum; ++$i) {

    $url = "http://api.somesite.com&page_number=$i";

    $response = json_decode(file_get_contents($url), true);

    $resp = FlattenMultiArray($response, true, NULL);

    $myarray = array(

    'Page_id' => $i,  //your database table column name => data you want to insert
    'County' => $resp['county'],
    'Country' => $resp['country'],
    'Post_Town' => $resp['0post_town'],
);

    $mykeys = implode(', ', array_keys($myarray));
    
    $myplaceholders[] = '(' . implode (", ", array_fill(0, count($myarray), '?')) . ')';
    
    array_push($values, ...array_values($myarray));

}  //endfor

$res = $db->prepare("INSERT INTO YourDB ($mykeys) VALUES " . join(', ', $myplaceholders)) ;

$res->execute($values);

 

Link to comment
Share on other sites

The array flattening is just a waste of time and effort. Get the data from the multi-D array directly. It's not rocket science.

$myarray = array(

    'Page_id' => $i,  //your database table column name => data you want to insert
    'County' => $response['county'],
    'Country' => $response['country'],
    'Post_Town' => $response['listings'][0]['post_town']
);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.