Getting data from REST API (JSON), extract variables and upload those to database using MySQL











up vote
1
down vote

favorite












I'm working with a REST API, first time doing this and I have a working code but it's long and just looks messy to me. I just know there's a better and faster way to do it.



try {
$LOCATIONS = new PicqerFinancialsExactItemWarehouse($connection);
$LOCATIONS_GET = $LOCATIONS->get();
foreach($LOCATIONS_GET as $LOCATIONS){
$locationID = $LOCATIONS->ID;
$locationDefaultStorageLocationCode = $LOCATIONS->DefaultStorageLocationCode;
$locationDefaultStorageLocatoinDescription = $LOCATIONS->DefaultStorageLocationDescription;
$locationWarehouseCode = $LOCATIONS->WarehouseCode;
$locationDefaultStorageLocation = $LOCATIONS->DefaultStorageLocation;
$locationLocatieType = 0; //Locatie type

$LOCATIONS_CHECK = $conn->query("SELECT ID FROM data_exact_locations WHERE ID='$locationID' LIMIT 1");
if($LOCATIONS_CHECK->num_rows == 0){
$LOCATIONS_SQL = "INSERT INTO data_exact_locations (ID, Code, Omschrijving, Magazijn, Standaardlocatie, Locatie_type)
VALUES ('$locationID','$locationDefaultStorageLocationCode','$locationDefaultStorageLocatoinDescription', '$locationWarehouseCode', '$locationDefaultStorageLocation')";
if (mysqli_query($conn, $LOCATIONS_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}


This is what the code looks like, but this is a short version. There are also instances where I need to get something like 30 variables out of the JSON file, and upload those to a database.



Another example from the same code:



try {
$CRM = new PicqerFinancialsExactAccount($connection);
$CRM_GET = $CRM->filter("IsSupplier eq true");
foreach($CRM_GET as $CRM){
$crmID = $CRM->ID;
$crmCode = $CRM->Code;
$crmSearchCode =$CRM->SearchCode;
$crmName = $CRM->Name;
$crmAddressLine1 = $CRM->AddressLine1;
$crmAddressline2 = $CRM->AddressLine2;
$crmAddressline3 = $CRM->AddressLine3;
$crmVatNumber = $CRM->VATNumber;
$crmCountry = $CRM->Country;
$crmCity = $CRM->City;
$crmPostcode = $CRM->Postcode;
$crmState = $CRM->State;
$crmRemarks = $CRM->Remarks;

$CRM_CHECK = $conn->query("SELECT ID FROM data_exact_crm WHERE ID='$crmID' LIMIT 1");
if($CRM_CHECK->num_rows == 0){
$CRM_SQL = "INSERT INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES ('$crmID','$crmCode','$crmSearchCode','$crmName','$crmAddressLine1','$crmAddressline2','$crmAddressline3','$crmVatNumber','$crmCountry','$crmCity','$crmPostcode','$crmState','$crmRemarks')";
if (mysqli_query($conn, $CRM_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try Again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}









share|improve this question









New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
    – IEatBagels
    Nov 15 at 20:49










  • Yeah it works, i just thought it was really inefficient
    – FlubberBeer
    Nov 15 at 20:52










  • this is a short version - does it mean you've removed anything from it?
    – t3chb0t
    22 hours ago















up vote
1
down vote

favorite












I'm working with a REST API, first time doing this and I have a working code but it's long and just looks messy to me. I just know there's a better and faster way to do it.



try {
$LOCATIONS = new PicqerFinancialsExactItemWarehouse($connection);
$LOCATIONS_GET = $LOCATIONS->get();
foreach($LOCATIONS_GET as $LOCATIONS){
$locationID = $LOCATIONS->ID;
$locationDefaultStorageLocationCode = $LOCATIONS->DefaultStorageLocationCode;
$locationDefaultStorageLocatoinDescription = $LOCATIONS->DefaultStorageLocationDescription;
$locationWarehouseCode = $LOCATIONS->WarehouseCode;
$locationDefaultStorageLocation = $LOCATIONS->DefaultStorageLocation;
$locationLocatieType = 0; //Locatie type

$LOCATIONS_CHECK = $conn->query("SELECT ID FROM data_exact_locations WHERE ID='$locationID' LIMIT 1");
if($LOCATIONS_CHECK->num_rows == 0){
$LOCATIONS_SQL = "INSERT INTO data_exact_locations (ID, Code, Omschrijving, Magazijn, Standaardlocatie, Locatie_type)
VALUES ('$locationID','$locationDefaultStorageLocationCode','$locationDefaultStorageLocatoinDescription', '$locationWarehouseCode', '$locationDefaultStorageLocation')";
if (mysqli_query($conn, $LOCATIONS_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}


This is what the code looks like, but this is a short version. There are also instances where I need to get something like 30 variables out of the JSON file, and upload those to a database.



Another example from the same code:



try {
$CRM = new PicqerFinancialsExactAccount($connection);
$CRM_GET = $CRM->filter("IsSupplier eq true");
foreach($CRM_GET as $CRM){
$crmID = $CRM->ID;
$crmCode = $CRM->Code;
$crmSearchCode =$CRM->SearchCode;
$crmName = $CRM->Name;
$crmAddressLine1 = $CRM->AddressLine1;
$crmAddressline2 = $CRM->AddressLine2;
$crmAddressline3 = $CRM->AddressLine3;
$crmVatNumber = $CRM->VATNumber;
$crmCountry = $CRM->Country;
$crmCity = $CRM->City;
$crmPostcode = $CRM->Postcode;
$crmState = $CRM->State;
$crmRemarks = $CRM->Remarks;

$CRM_CHECK = $conn->query("SELECT ID FROM data_exact_crm WHERE ID='$crmID' LIMIT 1");
if($CRM_CHECK->num_rows == 0){
$CRM_SQL = "INSERT INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES ('$crmID','$crmCode','$crmSearchCode','$crmName','$crmAddressLine1','$crmAddressline2','$crmAddressline3','$crmVatNumber','$crmCountry','$crmCity','$crmPostcode','$crmState','$crmRemarks')";
if (mysqli_query($conn, $CRM_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try Again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}









share|improve this question









New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
    – IEatBagels
    Nov 15 at 20:49










  • Yeah it works, i just thought it was really inefficient
    – FlubberBeer
    Nov 15 at 20:52










  • this is a short version - does it mean you've removed anything from it?
    – t3chb0t
    22 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm working with a REST API, first time doing this and I have a working code but it's long and just looks messy to me. I just know there's a better and faster way to do it.



try {
$LOCATIONS = new PicqerFinancialsExactItemWarehouse($connection);
$LOCATIONS_GET = $LOCATIONS->get();
foreach($LOCATIONS_GET as $LOCATIONS){
$locationID = $LOCATIONS->ID;
$locationDefaultStorageLocationCode = $LOCATIONS->DefaultStorageLocationCode;
$locationDefaultStorageLocatoinDescription = $LOCATIONS->DefaultStorageLocationDescription;
$locationWarehouseCode = $LOCATIONS->WarehouseCode;
$locationDefaultStorageLocation = $LOCATIONS->DefaultStorageLocation;
$locationLocatieType = 0; //Locatie type

$LOCATIONS_CHECK = $conn->query("SELECT ID FROM data_exact_locations WHERE ID='$locationID' LIMIT 1");
if($LOCATIONS_CHECK->num_rows == 0){
$LOCATIONS_SQL = "INSERT INTO data_exact_locations (ID, Code, Omschrijving, Magazijn, Standaardlocatie, Locatie_type)
VALUES ('$locationID','$locationDefaultStorageLocationCode','$locationDefaultStorageLocatoinDescription', '$locationWarehouseCode', '$locationDefaultStorageLocation')";
if (mysqli_query($conn, $LOCATIONS_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}


This is what the code looks like, but this is a short version. There are also instances where I need to get something like 30 variables out of the JSON file, and upload those to a database.



Another example from the same code:



try {
$CRM = new PicqerFinancialsExactAccount($connection);
$CRM_GET = $CRM->filter("IsSupplier eq true");
foreach($CRM_GET as $CRM){
$crmID = $CRM->ID;
$crmCode = $CRM->Code;
$crmSearchCode =$CRM->SearchCode;
$crmName = $CRM->Name;
$crmAddressLine1 = $CRM->AddressLine1;
$crmAddressline2 = $CRM->AddressLine2;
$crmAddressline3 = $CRM->AddressLine3;
$crmVatNumber = $CRM->VATNumber;
$crmCountry = $CRM->Country;
$crmCity = $CRM->City;
$crmPostcode = $CRM->Postcode;
$crmState = $CRM->State;
$crmRemarks = $CRM->Remarks;

$CRM_CHECK = $conn->query("SELECT ID FROM data_exact_crm WHERE ID='$crmID' LIMIT 1");
if($CRM_CHECK->num_rows == 0){
$CRM_SQL = "INSERT INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES ('$crmID','$crmCode','$crmSearchCode','$crmName','$crmAddressLine1','$crmAddressline2','$crmAddressline3','$crmVatNumber','$crmCountry','$crmCity','$crmPostcode','$crmState','$crmRemarks')";
if (mysqli_query($conn, $CRM_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try Again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}









share|improve this question









New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm working with a REST API, first time doing this and I have a working code but it's long and just looks messy to me. I just know there's a better and faster way to do it.



try {
$LOCATIONS = new PicqerFinancialsExactItemWarehouse($connection);
$LOCATIONS_GET = $LOCATIONS->get();
foreach($LOCATIONS_GET as $LOCATIONS){
$locationID = $LOCATIONS->ID;
$locationDefaultStorageLocationCode = $LOCATIONS->DefaultStorageLocationCode;
$locationDefaultStorageLocatoinDescription = $LOCATIONS->DefaultStorageLocationDescription;
$locationWarehouseCode = $LOCATIONS->WarehouseCode;
$locationDefaultStorageLocation = $LOCATIONS->DefaultStorageLocation;
$locationLocatieType = 0; //Locatie type

$LOCATIONS_CHECK = $conn->query("SELECT ID FROM data_exact_locations WHERE ID='$locationID' LIMIT 1");
if($LOCATIONS_CHECK->num_rows == 0){
$LOCATIONS_SQL = "INSERT INTO data_exact_locations (ID, Code, Omschrijving, Magazijn, Standaardlocatie, Locatie_type)
VALUES ('$locationID','$locationDefaultStorageLocationCode','$locationDefaultStorageLocatoinDescription', '$locationWarehouseCode', '$locationDefaultStorageLocation')";
if (mysqli_query($conn, $LOCATIONS_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}


This is what the code looks like, but this is a short version. There are also instances where I need to get something like 30 variables out of the JSON file, and upload those to a database.



Another example from the same code:



try {
$CRM = new PicqerFinancialsExactAccount($connection);
$CRM_GET = $CRM->filter("IsSupplier eq true");
foreach($CRM_GET as $CRM){
$crmID = $CRM->ID;
$crmCode = $CRM->Code;
$crmSearchCode =$CRM->SearchCode;
$crmName = $CRM->Name;
$crmAddressLine1 = $CRM->AddressLine1;
$crmAddressline2 = $CRM->AddressLine2;
$crmAddressline3 = $CRM->AddressLine3;
$crmVatNumber = $CRM->VATNumber;
$crmCountry = $CRM->Country;
$crmCity = $CRM->City;
$crmPostcode = $CRM->Postcode;
$crmState = $CRM->State;
$crmRemarks = $CRM->Remarks;

$CRM_CHECK = $conn->query("SELECT ID FROM data_exact_crm WHERE ID='$crmID' LIMIT 1");
if($CRM_CHECK->num_rows == 0){
$CRM_SQL = "INSERT INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES ('$crmID','$crmCode','$crmSearchCode','$crmName','$crmAddressLine1','$crmAddressline2','$crmAddressline3','$crmVatNumber','$crmCountry','$crmCity','$crmPostcode','$crmState','$crmRemarks')";
if (mysqli_query($conn, $CRM_SQL)){
echo "Worked! <BR>";
} else{
echo ("Try Again! <BR>" . mysqli_error($conn));
}
} else {
echo ("Already in database! <BR>");
}
}
} catch (Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}






php mysql json database






share|improve this question









New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 15 at 3:54









Jamal

30.2k11115226




30.2k11115226






New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 14 at 11:18









FlubberBeer

83




83




New contributor




FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






FlubberBeer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
    – IEatBagels
    Nov 15 at 20:49










  • Yeah it works, i just thought it was really inefficient
    – FlubberBeer
    Nov 15 at 20:52










  • this is a short version - does it mean you've removed anything from it?
    – t3chb0t
    22 hours ago


















  • I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
    – IEatBagels
    Nov 15 at 20:49










  • Yeah it works, i just thought it was really inefficient
    – FlubberBeer
    Nov 15 at 20:52










  • this is a short version - does it mean you've removed anything from it?
    – t3chb0t
    22 hours ago
















I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
– IEatBagels
Nov 15 at 20:49




I might be wrong, but does this code work? The value parameters are string representations of your variable name and I'm not sure this works in PHP (or in a lot of languages)
– IEatBagels
Nov 15 at 20:49












Yeah it works, i just thought it was really inefficient
– FlubberBeer
Nov 15 at 20:52




Yeah it works, i just thought it was really inefficient
– FlubberBeer
Nov 15 at 20:52












this is a short version - does it mean you've removed anything from it?
– t3chb0t
22 hours ago




this is a short version - does it mean you've removed anything from it?
– t3chb0t
22 hours ago










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










PDO has a great feature for you, it can accept an array with parameters for execute(). It means you won't have to extract separate variables anymore.



So just use PDO instead of mysqli and your code will become just two lines



$CRM_GET = $CRM->filter("IsSupplier eq true");
$stmt = "INSERT IGNORE INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES (:ID,:Code, :SearchCode,:Name,:AddressLine1,:Addressline2,:Addressline3,:VatNumber,:Country,:City,:Postcode,:State,:Remarks)";
foreach($CRM_GET as $CRM) {
$stmt->execute((array)$CRM);
}


Notice that I am using quite a few tricks here:




  • an object is converted to an array for PDO

  • prepare is called only once which makes your queries executed faster (you can read on that from my article I linked above)

  • assuming ID is a primary key, there is no need to run a select query. Just add a keyword IGNORE to the INSERT statement

  • your error handling code is overkill. if you leave an exception alone, it will tell you exactly the same information you are echoing manually. So get rid of try/catch as well

  • of course such output as Worked! or Try again after each query execution is not informative ans should be removed as well






share|improve this answer























  • Thank for the tips
    – FlubberBeer
    Nov 14 at 15:21











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






FlubberBeer is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207646%2fgetting-data-from-rest-api-json-extract-variables-and-upload-those-to-databas%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










PDO has a great feature for you, it can accept an array with parameters for execute(). It means you won't have to extract separate variables anymore.



So just use PDO instead of mysqli and your code will become just two lines



$CRM_GET = $CRM->filter("IsSupplier eq true");
$stmt = "INSERT IGNORE INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES (:ID,:Code, :SearchCode,:Name,:AddressLine1,:Addressline2,:Addressline3,:VatNumber,:Country,:City,:Postcode,:State,:Remarks)";
foreach($CRM_GET as $CRM) {
$stmt->execute((array)$CRM);
}


Notice that I am using quite a few tricks here:




  • an object is converted to an array for PDO

  • prepare is called only once which makes your queries executed faster (you can read on that from my article I linked above)

  • assuming ID is a primary key, there is no need to run a select query. Just add a keyword IGNORE to the INSERT statement

  • your error handling code is overkill. if you leave an exception alone, it will tell you exactly the same information you are echoing manually. So get rid of try/catch as well

  • of course such output as Worked! or Try again after each query execution is not informative ans should be removed as well






share|improve this answer























  • Thank for the tips
    – FlubberBeer
    Nov 14 at 15:21















up vote
1
down vote



accepted










PDO has a great feature for you, it can accept an array with parameters for execute(). It means you won't have to extract separate variables anymore.



So just use PDO instead of mysqli and your code will become just two lines



$CRM_GET = $CRM->filter("IsSupplier eq true");
$stmt = "INSERT IGNORE INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES (:ID,:Code, :SearchCode,:Name,:AddressLine1,:Addressline2,:Addressline3,:VatNumber,:Country,:City,:Postcode,:State,:Remarks)";
foreach($CRM_GET as $CRM) {
$stmt->execute((array)$CRM);
}


Notice that I am using quite a few tricks here:




  • an object is converted to an array for PDO

  • prepare is called only once which makes your queries executed faster (you can read on that from my article I linked above)

  • assuming ID is a primary key, there is no need to run a select query. Just add a keyword IGNORE to the INSERT statement

  • your error handling code is overkill. if you leave an exception alone, it will tell you exactly the same information you are echoing manually. So get rid of try/catch as well

  • of course such output as Worked! or Try again after each query execution is not informative ans should be removed as well






share|improve this answer























  • Thank for the tips
    – FlubberBeer
    Nov 14 at 15:21













up vote
1
down vote



accepted







up vote
1
down vote



accepted






PDO has a great feature for you, it can accept an array with parameters for execute(). It means you won't have to extract separate variables anymore.



So just use PDO instead of mysqli and your code will become just two lines



$CRM_GET = $CRM->filter("IsSupplier eq true");
$stmt = "INSERT IGNORE INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES (:ID,:Code, :SearchCode,:Name,:AddressLine1,:Addressline2,:Addressline3,:VatNumber,:Country,:City,:Postcode,:State,:Remarks)";
foreach($CRM_GET as $CRM) {
$stmt->execute((array)$CRM);
}


Notice that I am using quite a few tricks here:




  • an object is converted to an array for PDO

  • prepare is called only once which makes your queries executed faster (you can read on that from my article I linked above)

  • assuming ID is a primary key, there is no need to run a select query. Just add a keyword IGNORE to the INSERT statement

  • your error handling code is overkill. if you leave an exception alone, it will tell you exactly the same information you are echoing manually. So get rid of try/catch as well

  • of course such output as Worked! or Try again after each query execution is not informative ans should be removed as well






share|improve this answer














PDO has a great feature for you, it can accept an array with parameters for execute(). It means you won't have to extract separate variables anymore.



So just use PDO instead of mysqli and your code will become just two lines



$CRM_GET = $CRM->filter("IsSupplier eq true");
$stmt = "INSERT IGNORE INTO data_exact_crm (ID, Code, SearchCode, Name, AddressLine1, AddressLine2, AddressLine3, VATNumber, CountryDescription, City, PostCode, StateDescription, Remarks)
VALUES (:ID,:Code, :SearchCode,:Name,:AddressLine1,:Addressline2,:Addressline3,:VatNumber,:Country,:City,:Postcode,:State,:Remarks)";
foreach($CRM_GET as $CRM) {
$stmt->execute((array)$CRM);
}


Notice that I am using quite a few tricks here:




  • an object is converted to an array for PDO

  • prepare is called only once which makes your queries executed faster (you can read on that from my article I linked above)

  • assuming ID is a primary key, there is no need to run a select query. Just add a keyword IGNORE to the INSERT statement

  • your error handling code is overkill. if you leave an exception alone, it will tell you exactly the same information you are echoing manually. So get rid of try/catch as well

  • of course such output as Worked! or Try again after each query execution is not informative ans should be removed as well







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 17:04









Sᴀᴍ Onᴇᴌᴀ

7,67561748




7,67561748










answered Nov 14 at 13:45









Your Common Sense

3,186526




3,186526












  • Thank for the tips
    – FlubberBeer
    Nov 14 at 15:21


















  • Thank for the tips
    – FlubberBeer
    Nov 14 at 15:21
















Thank for the tips
– FlubberBeer
Nov 14 at 15:21




Thank for the tips
– FlubberBeer
Nov 14 at 15:21










FlubberBeer is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















FlubberBeer is a new contributor. Be nice, and check out our Code of Conduct.













FlubberBeer is a new contributor. Be nice, and check out our Code of Conduct.












FlubberBeer is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207646%2fgetting-data-from-rest-api-json-extract-variables-and-upload-those-to-databas%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre