ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Mysql Update in NodeJs

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='22' AND `tutorial_submenu`='1395' AND `tutorial_status`=1 LIMIT 1

Mysql Update in NodeJs

To update records in MySQL using Node.js, you can use the UPDATE SQL statement with either the mysql or mysql2 package.

Here’s a complete example to get you going 👇


✅ Step 1: Install MySQL package

bash

npm install mysql const mysql = require('mysql');const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase'});connection.connect();const sql = 'UPDATE users SET age = ? WHERE name = ?';const values = [32, 'Bob'];connection.query(sql, values, (err, result) => { if (err) throw err; console.log('Rows updated:', result.affectedRows); connection.end();});


Using mysql2 (Async/Await)

js

const mysql = require('mysql2/promise');async function updateUser() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase' }); const sql = 'UPDATE users SET age = ? WHERE name = ?'; const values = [28, 'Alice']; const [result] = await connection.execute(sql, values); console.log('Rows updated:', result.affectedRows); await connection.end();}updateUser();


🧠 Tips

  • Always use ? placeholders to prevent SQL injection.

  • Check result.affectedRows to confirm how many rows were updated.

  • You can update multiple fields like:

sql

UPDATE users SET name = ?, age = ? WHERE id = ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql