Curs Javascript

- Obiecte cu Randuri si Coloane
- Creare tabel HTML cu date din Select

Select in tabel MySQL

Pentru a face select intr-un tabel din baza de date MySQL, se foloseste instructiunea "SELECT".
- In acest exemplu vom selecta randurile cu ID-ul intre 1 si 4 din tabelul "friends" si vom afisa obiectul returnat:
const mysql = require('mysql');

const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

let sql ='SELECT * FROM friends WHERE id BETWEEN 1 AND 4';

pool.query(sql, (err, res, cols)=>{
  if(err) throw err;
  console.log(res);
});
Salveaza codul de mai sus intr-un fisier "mysql_select.js si ruleaza fisierul:
node test/mysql_select.js
Vei obtine un rezultat ca acesta:
[
  { id: 1, name: 'Olpram', address: 'Heaven, Peace 0'},
  { id: 2, name: 'Xela', address: 'Good 71'},
  { id: 3, name: 'Rotciv', address: 'Helpful 4'},
  { id: 4, name: 'Noi', address: 'Loving st 652'}
]

Obiecte cu Randuri si Coloane

1. Obiectul result este un array cu fiecare rand ca un obiect.
Pentru a returna de ex. valoarea din coloana "address" din al doilea rand, faceti referire la proprietatea "address" al obiectului din al doilea array (primul rand are index 0).
console.log(res[1].address);
Iti va da un rezultat ca acesta:
Good 71

2. Al treilea parametru a functiei callback (aici "cols") este un array cu obiecte ce contin informatii despre fiecare coloana din rezultat.
Daca adaugi acest cod in functia callback a metodei query():
console.log(cols[0]);
- Vei obtine un obiect cu informatii despre prima coloana:
{
  catalog: 'def',
  db: 'nodedb',
  table: 'friends',
  orgTable: 'friends',
  name: 'id',
  orgName: 'id',
  charsetNr: 33,
  length: 11,
  type: 3,
  flags: 16899,
  decimals: 0,
  default: undefined,
  zeroFill: false,
  protocol41: true
}
Pentru a obtine numele coloanei, folositi proprietatea "name":
var fname = fields[0].name;

Creare tabel HTML cu date din Select

Aici aveti un cod Node.js care selecteaza date din MySQL, ordoneaza rezultatul dupa nume si afiseaza rezultatul intr-un tabel html.
const http = require('http');
const mysql = require('mysql');

const pool = mysql.createPool({
  host: '127.0.0.1',
  user: 'root',
  password: 'pass',
  database: 'nodedb',
  charset: 'utf8'
});

//html string that will be send to browser
var reo ='<html><head><title>Node.js MySQL Select</title></head><body><h1>Node.js MySQL Select</h1>{${table}}</body></html>';

//sets and returns html table with results from sql select
//Receives sql query and callback function to return the table
function setResHtml(sql, cb){
  pool.getConnection((err, con)=>{
    if(err) throw err;

    con.query(sql, (err, res, cols)=>{
      if(err) throw err;

      var table =''; //to store html table

      //create html table with data from res.
      for(var i=0; i<res.length; i++){
        table +='<tr><td>'+ (i+1) +'</td><td>'+ res[i].name +'</td><td>'+ res[i].address +'</td></tr>';
      }
      table ='<table border="1"><tr><th>Nr.</th><th>Name</th><th>Address</th></tr>'+ table +'</table>';

      con.release(); //Done with mysql connection

      return cb(table);
    });
  });
}

let sql ='SELECT name, address FROM friends WHERE id >1 ORDER BY name';

//create the server for browser access
const server = http.createServer((req, res)=>{
  setResHtml(sql, resql=>{
    reo = reo.replace('{${table}}', resql);
    res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
    res.write(reo, 'utf-8');
    res.end();
  });
});

server.listen(8080, ()=>{
  console.log('Server running at //localhost:8080/');
});
Salveaza codul de mai sus intr-un fisier numit "mysql_res_html.js" si ruleaza fisierul:
node test/mysql_res_html.js
Deschide browserul si acceseaza adresa: //localhost:8080/
Va afisa o pagina ca in aceasta imagine:
HTML Table MySQL Select

Un Test simplu in fiecare zi

HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Ce tag se foloseste pentru a crea o caseta de adaugare text?
<form> <input> <div>
<input type="text" name="a_name" value="val" />
Ce proprietate CSS se foloseste pentru a afisa textul cu majuscule-mici?
display font-variant font-style
h3 {
  font-variant: small-caps;
}
Ce instructiune afiseaza o fereastra cu un mesaj in ea?
for() Date() alert()
var msg = "Viziteaza CoursesWeb.net";
alert(msg);
Indicati codul PHP prin care se obtine IP-ul utilizatorului.
$_SERVER["HTTP_USER_AGENT"] $_SERVER["REMOTE_ADDR"] $_GET[]
$ip = $_SERVER["REMOTE_ADDR"];
echo $ip;
Cum se traduce cuvantul "actually"?
actual de fapt acum
He actually came on time.
- De fapt, el a venit la timp.
Cum se traduce expresia: "Hace buen tiempo"?
Face timp E vreme frumoasa Drum bun
Hoy hace buen tiempo.
- Astazi e vreme frumoasa.
Select in MySQL, Creare tabel HTML cu date din Select

Last accessed pages

  1. Obiectul Date - Lucru cu Data si Timp (1119)
  2. Sintaxa JavaScript (11278)
  3. Curs si Tutoriale JavaScript (134964)
  4. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (277829)
  5. Verbe modale - Exercitii incepatori (13518)

Popular pages this month

  1. Curs HTML gratuit Tutoriale HTML5 (1334)
  2. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (1327)
  3. Coduri pt culori (948)
  4. Creare si editare pagini HTML (521)
  5. Gramatica limbii spaniole. Indrumator si prezentare generala (378)