Reputation: 33644
I have the following code:
connection.query("SELECT * FROM accounts_data WHERE new_username LIKE '%" + username + "%';", function (error, results, fields) {
});
and I am getting the following error
SELECT * FROM `accounts_data` WHERE `new_username` LIKE '%preloved_bys%'
^^^^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
what is wrong with the query? when i execute it via phpmyadmin it works just fine
Upvotes: 1
Views: 1545
Reputation: 7150
It seems there is nothing wrong with the following code.
but fyi, SyntaxError: Unexpected identifier
occurs due to some reasons, mentioned below:
case-sensitive
language when you pass any parameter into it like 'new_username'
. Recheck it should be correct.' or "
invalid identifier
. let str = '%'+username+'%';
let sql = "SELECT * FROM accouts_data WHERE new_username LIKE ?";
connection.query(sql, str, function(err, result) {
if(err) throw err
});
npm install
It'll update every module with their latest version automatically.
Upvotes: 2
Reputation: 3710
I think you should use proper escaping provided by mysql
lib:
connection.query(
"SELECT * FROM accounts_data WHERE new_username LIKE ?; ",
['%' + username + '%'],
function (error, results, fields) { ... }
);
Also check your mysql
version both on client (npm package) and server side.
For debugging, I often use console.log(connection.format(query, values));
Upvotes: 1
Reputation: 99
You can use concat example
connection.query("SELECT * FROM accounts_data WHERE new_username LIKE concat('%',?,'%');", [username],(error, results, fields)=>{console.log(result)})
Upvotes: 0
Reputation: 100
i get success with do following :
var value = 'sc188203395acc255';
connection.query('SELECT * from emp where empid like ?', '%' + value + '%', ...)
Turn on debugging in the driver (pass debug:true
as argument to mysql.createConnection
), it doesn't escape the percent sign
One More Way to Slove this Error :
"SELECT * FROM emp WHERE name empid " + connection.escape('%'+value+'%')
use connection.escape alternate way to do same things
One More Way to Solve this Error :
reinstall module on your project because sometimes error can be solved by reinstalling it
delete node_modules
and package-lock.json
and last run npm install
Upvotes: 2