Getting data from REST API (JSON), extract variables and upload those to database using MySQL [on hold]
up vote
0
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();
}
php mysql json database
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.
put on hold as off-topic by Toby Speight, Graipher, vnp, t3chb0t, Quill 53 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, t3chb0t, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
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();
}
php mysql json database
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.
put on hold as off-topic by Toby Speight, Graipher, vnp, t3chb0t, Quill 53 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, t3chb0t, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
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
1
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
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();
}
php mysql json database
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
php mysql json database
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.
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
33
33
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.
put on hold as off-topic by Toby Speight, Graipher, vnp, t3chb0t, Quill 53 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, t3chb0t, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Toby Speight, Graipher, vnp, t3chb0t, Quill 53 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Toby Speight, Graipher, vnp, t3chb0t, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.
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
1
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days ago
add a comment |
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
1
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days 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
1
1
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days ago
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
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
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:Uncaught Error: Call to a member function execute()
– FlubberBeer
13 hours ago
Code :try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }
– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
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
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:Uncaught Error: Call to a member function execute()
– FlubberBeer
13 hours ago
Code :try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }
– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
add a comment |
up vote
2
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
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:Uncaught Error: Call to a member function execute()
– FlubberBeer
13 hours ago
Code :try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }
– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
add a comment |
up vote
2
down vote
accepted
up vote
2
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
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
edited Nov 14 at 17:04
Sᴀᴍ Onᴇᴌᴀ
7,71061748
7,71061748
answered Nov 14 at 13:45
Your Common Sense
3,211526
3,211526
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:Uncaught Error: Call to a member function execute()
– FlubberBeer
13 hours ago
Code :try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }
– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
add a comment |
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:Uncaught Error: Call to a member function execute()
– FlubberBeer
13 hours ago
Code :try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }
– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Thank for the tips
– FlubberBeer
Nov 14 at 15:21
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:
Uncaught Error: Call to a member function execute() – FlubberBeer
13 hours ago
Hi, I'm trying to implement your answer into my code. But i keep getting this error message:
Uncaught Error: Call to a member function execute() – FlubberBeer
13 hours ago
Code :
try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }– FlubberBeer
13 hours ago
Code :
try{ $CRM = new PicqerFinancialsExactAccount($connection); $CRM_GET = $CRM->filter("IsSupplier eq true"); $CRM_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){ $CRM_STMT->execute((array)$CRM); } }– FlubberBeer
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
"So just use PDO instead of mysqli"
– Your Common Sense
13 hours ago
add a comment |
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
1
this is a short version - does it mean you've removed anything from it?
– t3chb0t
2 days ago