node.js取出select的结果

/**
 * Created by web-01 on 2018/1/4.
 */
const mysql=require("mysql");
var pool=mysql.createPool({
    host:'127.0.0.1',
    user:"root",
    password:"",
    database:"test",
    port:3306,
    connectionLimit:25 //连接数量限制
});
//连接池会常驻内存,所以程序不会停止

var sql="SELECT * FROM test WHERE tid=3";
pool.getConnection((err,conn)=>{
    conn.query(sql,(err,res)=>{
        console.log(res);
        console.log(res[0]);
        console.log(res[0].tid);
        console.log(res[0].tname);
        //释放连接 必须要释放,否则很快占满连接数!!!
        conn.release();
    });
});

你可以通过以下步骤实现: 1.Node.js中使用相应的数据库模块(如mysql、mongodb等)连接到数据库。 2. 编写查询语句,从数据库中查询需要的数据。 3. 将查询结果JSON格式返回给客户端。 4. 在客户端使用JavaScript解析JSON数据,并将数据以表格的形式展示出来。 以下是一个示例代码: Node.js代码: ```javascript const mysql = require(&#39;mysql&#39;); const connection = mysql.createConnection({ host: &#39;localhost&#39;, user: &#39;root&#39;, password: &#39;password&#39;, database: &#39;test&#39; }); connection.connect(); const sql = &#39;SELECT * FROM users&#39;; connection.query(sql, (error, results, fields) => { if (error) throw error; console.log(&#39;The solution is: &#39;, results); }); connection.end(); ``` 这段代码连接到本地MySQL数据库,并查询名为“users”的表格中的所有数据。查询结果将在控制台中打印出来。 客户端HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Table Example</title> </head> <body> <table id="users-table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> </tbody> </table> <script> fetch(&#39;/users&#39;) .then(response => response.json()) .then(data => { const tableBody = document.querySelector(&#39;#users-table tbody&#39;); data.forEach(user => { const row = document.createElement(&#39;tr&#39;); const idCell = document.createElement(&#39;td&#39;); const nameCell = document.createElement(&#39;td&#39;); const ageCell = document.createElement(&#39;td&#39;); idCell.textContent = user.id; nameCell.textContent = user.name; ageCell.textContent = user.age; row.appendChild(idCell); row.appendChild(nameCell); row.appendChild(ageCell); tableBody.appendChild(row); }); }); </script> </body> </html> ``` 这段代码使用JavaScript的Fetch API向服务器端发起请求,并将返回的JSON数据解析为表格展示出来。在此示例中,服务器端的路由应该为“/users”,并且返回JSON格式的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值