Home  >  Q&A  >  body text

NextJS Mysql runs fine via phpmyadmin but has issues querying via api

<p>As the title says, my SQL code works in phpmyadmin but not when requesting from the API. My API uses the mysql package: </p> <pre class="brush:php;toolbar:false;">npm i mysql</pre> <p>SQL code:</p> <pre class="brush:php;toolbar:false;">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;</pre> <p>This is completely normal in phpmyadmin.</p> <p>我使用以下方式调用函数:</p> <pre class="brush:php;toolbar:false;">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); } );</pre> <p>显示的错误是:</p> <pre class="brush:php;toolbar:false;">{"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;"}</pre> <p>有人知道这可能是什么原因引起的吗?任何帮助将不胜感激 :)</p>
P粉549986089P粉549986089420 days ago392

reply all(1)I'll reply

  • P粉037450467

    P粉0374504672023-08-27 10:14:45

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

    By adding

    {multipleStatements: true,}

    into my connection pool configuration and it worked like magic :)

    reply
    0
  • Cancelreply