Created
May 28, 2023 13:31
-
-
Save fovelas/a50848c2bb2d0ddaeead94d3e43f74f3 to your computer and use it in GitHub Desktop.
Node.js ile MySQL Bağlantısı
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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