Search code examples
mysqlnode.js

nodejs throws error with mysql like query via prepared statements


Am retrieving values from database using nodejs. I implemented mysql like query via prepared statement to ensure that sql injection attack is eliminated. my problem is that it does not retrieve any result. it just show empty results in the console please can someone point to me what is wrong with the query

exports.autosearch = function (req, res) {
//var search = req.body.searchText;

var search = 'bukatti';
//db.query('SELECT * FROM users WHERE name like ?', ['%' + search + '%'], function (error, results, fields) {


db.query('SELECT * FROM users WHERE name like ?', ['%search%'], function (error, results, fields) {
console.log(results);






});



}

Thanks


Solution

  • I have found out my problem. i added the error log and discover that the was type error somewhere. This fix it anyway

    db.query("SELECT * FROM users WHERE name like ?", ['%' + search + '%'], function (error, results, fields) {
    

    Thanks