Skip to content

Instantly share code, notes, and snippets.

@fovelas
Created May 28, 2023 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fovelas/a50848c2bb2d0ddaeead94d3e43f74f3 to your computer and use it in GitHub Desktop.
Save fovelas/a50848c2bb2d0ddaeead94d3e43f74f3 to your computer and use it in GitHub Desktop.
Node.js ile MySQL Bağlantısı
const mysql = require('mysql');
// database ayarları
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'database_name'
});
// yukarıdaki bilgilere göre database bağlanıyoruz
db.connect(function (err) {
// eğer bir hata oluşursa programı durdurup hatayı konsola yazdıracak
if (err) throw err;
// başarılı bir şekilde bağlandığını konsola yazdırıyoruz
console.log('connection successful');
});
// db objesini export ediyoruz ki bu dosyayı çağırdığımız yerde sorgular çalıştırabilelim
module.exports = db;
// database dosyamızı çağırıyoruz
const db = require('./database.js');
// sorgu çalıştırıyoruz
db.query(`SELECT * FROM players WHERE id = '107861148515'`, (err, result) => {
if (err) throw err;
var row = result[0];
if (result.length > 0) {
console.log(row);
} else {
console.log('player not found');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment