Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
NextJS Mysql INSERT works with commands through phpmyadmin but not with query from api

Like the title says, my sql code works in phpmyadmin but not with requiest from api. My api uses mysql package:

npm i mysql

SQL Code:

BEGIN;
INSERT INTO Addresses (address, postal_code, city, state, country) 
    VALUES('Something', 69420, 'Something', 'Something', 'Something');
SELECT LAST_INSERT_ID() INTO @mysql_address_id;
INSERT INTO Companies (owner, org_nr, name)
    VALUES('someid', 133769420, 'Something');
SELECT LAST_INSERT_ID() INTO @mysql_company_id;
INSERT INTO Users (company, member, administrator)
    VALUES(@mysql_company_id, 'someid', 1);
INSERT INTO CompanyDetails (shippingAddress, company, shortName, ourReference)
    VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something');
COMMIT;

And this works totaly fine in phpmyadmin.

I call the function using:

db.query(
          `BEGIN; INSERT INTO Addresses (address, postal_code, city, state, country) VALUES('Something', 69420, 'Something', 'Something', 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_address_id; INSERT INTO Companies (owner, org_nr, name) VALUES('Something', 133769420, 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_company_id; INSERT INTO Users (company, member, administrator) VALUES(@mysql_company_id, 'someid', 1); INSERT INTO CompanyDetails (shippingAddress, company, shortName, ourReference) VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something'); COMMIT;`,
          (err, result) => {
            if (err) return res.status(422).json(err);
            return res.status(200).json(result);
          }
        );

The error that is shown is:

{"code":"ER_PARSE_ERROR","errno":1064,"sqlMessage":"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO Addresses (address, postal_code, city, state, country) VALUES('Ve...' at line 1","sqlState":"42000","index":0,"sql":"BEGIN; INSERT INTO Addresses (address, postal_code, city, state, country) VALUES('Something', 69420, 'Something', 'Something', 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_address_id; INSERT INTO Companies (owner, org_nr, name) VALUES('someid', 133769420, 'Something'); SELECT LAST_INSERT_ID() INTO @mysql_company_id; INSERT INTO Users (company, member, administrator) VALUES(@mysql_company_id, 'someid', 1); INSERT INTO CompanyDetails (shippingAddress, company, shortName, ourReference) VALUES(@mysql_address_id, @mysql_company_id, 'Something', 'Something'); COMMIT;"}

Do anyone know what this might be caused by? Any help is appriciated :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found the solution here: https://stackoverflow.com/a/23267627/13214923 I turns out that node-mysql don't support multiple statements by default.

by adding

{multipleStatements: true,} 

To my pool config it worked like a charm :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!