How to Connect Node js to MySQL Database and Fetch Data in 5 minutes

Поделиться
HTML-код
  • Опубликовано: 2 янв 2025

Комментарии • 56

  • @LinXiaoChaun
    @LinXiaoChaun 2 года назад +7

    At [02:39], I was at first thrown off by the destructuring syntax
    const {createPool}=require('mysql');
    const pool = createPool( { … } ) ;
    An equivalent (more pedestrian) alternative would be something like:
    const mysql = require('mysql') ;
    const pool = mysql.createPool( { …} )

  • @MsamerieJohnson
    @MsamerieJohnson Год назад +8

    For those getting undefined, I used this code to connect to the database
    const mysql = require("mysql")
    //Create a Database Connection
    const con = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'your_password',
    database:'your_databse'
    })
    con.connect((err) => {
    if(err)
    {
    console.log(err)
    }else{
    console.log("CONNECTION SUCCESSFUL!")
    }
    })
    // Get The Data From The Table
    con.query('select * from your_table', (err, res) =>{
    return console.log(res)
    })

  • @תמרנידאם
    @תמרנידאם 2 года назад +1

    תותח אלוףףףףףף
    ההסבר ממש מעולה ועוזר
    תודה!🙏

  • @nerolifong1824
    @nerolifong1824 2 года назад +1

    short video and to the point. great job

  • @williamdevonshire356
    @williamdevonshire356 Год назад

    awesome. finally i can connect the two!!! woo yea!!! Thank you sir :)

  • @phantomak3r477
    @phantomak3r477 Год назад

    Quick and straight to the point.

  • @DiscussedFir6
    @DiscussedFir6 2 года назад +1

    Thank you so much for this, great tutorial!

  • @Sukubus666
    @Sukubus666 Год назад +3

    For me it keeps saying 'undefined'. 😭 Could you help out how to fix that issue? I have node.js installed and triple checked if my code looks the same like in video.. 😕

  • @axsd1489
    @axsd1489 Год назад +1

    Thanks for help

  • @Sami_Ljimari
    @Sami_Ljimari 3 года назад +2

    very useful, thank you for making the video

  • @rinkijain4517
    @rinkijain4517 2 месяца назад

    Greate sir

  • @bryantgarcia998
    @bryantgarcia998 Год назад +1

    Really useful video. By any chance, do you have a follow up video on how to post data to mysql work bench in javascript as well?

  • @michelsolis6603
    @michelsolis6603 Год назад

    Thanks! And then? When you want to upload proyect to prod? What do I have to do to hide this information?

  • @realhistory765
    @realhistory765 2 года назад

    Thank you bro ❤

  • @CorreaLobo
    @CorreaLobo 3 года назад +1

    Hello Brother! Excellent Video, You just got a sub thank you so much. So simple and understandable. TY ❤❤

  • @dimitriskosse1727
    @dimitriskosse1727 Год назад

    thank you bro

  • @assetdev1859
    @assetdev1859 2 года назад

    hey! great video. i have a question, if i want to deploy this website on vercel/heroku what I need to do? our sql data base located in localhost how correctly i understand so can we push it somewhere? something like cloud storage? thanks for the answer :3

  • @nimeshpatilvlogs8629
    @nimeshpatilvlogs8629 2 года назад

    Do we have to first create table insql

  • @wildriftgames1931
    @wildriftgames1931 2 года назад +1

    why didn't work on my visual studio code when I'll run in terminal? Pls. help!!

  • @_dejvy
    @_dejvy 3 года назад

    really good tutorial, just one question, how can i default select database name

  • @SITPonrajA
    @SITPonrajA Год назад

    Hey I have a error on require() function

  • @forester1
    @forester1 2 года назад

    Can we query same like php api ?

  • @yourmoveatheists
    @yourmoveatheists 2 года назад

    🐐 Thank you!

  • @Maumau369
    @Maumau369 Год назад

    is require not defined anymore? i get "ReferenceError: require is not defined"

  • @VarshaSivaramakrishnan
    @VarshaSivaramakrishnan Год назад

    i applied your code but it just displays undefined what do i do?

  • @goodluckoriuwa1669
    @goodluckoriuwa1669 Год назад

    Can uou show how to connect node express app to online MySQL database

  • @Dlo2018
    @Dlo2018 Год назад

    how do i create a node module?

  • @leonbou3959
    @leonbou3959 2 года назад +3

    Hi, i want to store the return of the query in a variable. how can i make that work? When i write const x = (query) and instead of return console.log() just returning the res. It doesn't gave me back the result. Can anyone help me how to get the res returned to store it in a variable?

    • @LinXiaoChaun
      @LinXiaoChaun 2 года назад

      In your code, before the call to pool.query, create a variable, say, foo, then assign it the value of res within the function body:
      var foo=[] ; pool.query( 'SELECT * FROM apidb.users', (err,res)=>{foo=Array.from(res)} ) ;
      The value of foo will now be the result of the mysql query.
      To confirm that this code is working, run node in your shell, entering REPL mode, then give it the REPL command '.load database.js'. Node will echo the content of the file database.js followed by the result of running that file.
      You will remain in REPL mode: you can give the REPL command "foo", and node will echo its value.
      End the session by typing

    • @sh44ko58
      @sh44ko58 2 года назад

      @@LinXiaoChaun Why is it not working for me ? Btw i ive coded also something with react so dont be confused ;D
      const pool = createPool({
      host: "localhost",
      user: "root",
      password: ""
      })
      var foo = []
      pool.query(`SELECT * FROM appclone.user`, (err, res) => {foo=Array.from(res)} )
      res.json({"users": [foo] })
      Could you help me ?

  • @mukkt
    @mukkt 2 года назад +2

    For anyone getting an authentication error:
    "npm install mysql2" then change the require statement to "mysql2"

  • @forester1
    @forester1 2 года назад

    Greate

  • @jasonmiller1076
    @jasonmiller1076 2 года назад +4

    mine said undefined

  • @kashish9220
    @kashish9220 3 года назад +5

    "undefined" occur as output of node database.js
    please help regarding this

    • @saadbinwasi3038
      @saadbinwasi3038 3 года назад

      same did you find solution?

    • @nirmalgaur3228
      @nirmalgaur3228 2 года назад

      Maybe your password is not "rootuser".

    • @timhoward5
      @timhoward5 2 года назад

      @@nirmalgaur3228 Nope, because mines did the same thing and I used my password.

    • @mikahoy
      @mikahoy 2 года назад

      It occurs because you haven't select the database where the table reside

    • @Pan_Jan_Wroclawski
      @Pan_Jan_Wroclawski 2 года назад +6

      try to use mysql2 instead of mysql package

  • @Aman22506
    @Aman22506 2 года назад

    can this apply in phpmyadmin

  • @joeyGunz1-w1r
    @joeyGunz1-w1r 2 года назад

    I am getting a 504 error when sending data to mysql in nginx

  • @koggism
    @koggism Год назад +1

    video is blured

    • @KindsonTheTechPro
      @KindsonTheTechPro  3 месяца назад

      I guess it is playing in lower resolution in your system, you need to play in a higher resolution

  • @rezaeiabazar5324
    @rezaeiabazar5324 Год назад

    ok