1

I'm new in the use ExpressJs, I created my own project to train myself. I would like to do a search bar so perform a select query with MySQL.

Here's my code :

let query = req.params.q;

console.log(query);

//var sql = "SELECT * FROM Movies WHERE movie_title like '%" + query + "%' OR movie_originalTitle like '%"+ query+"%'";

var sql = "SELECT * FROM Movies WHERE movie_originalTitle like ? OR movie_title like ?";
var values = [
    ['%' + query + '%', '%' + query + '%']
];

console.log(values[0]);

connection.query(sql, [values], (err, result, fields) => {
    console.log(sql);
    if (err) {
        throw err;
    };

    JSON.stringify(result);
    res.send({ movies: result });
});

I would like to do it with '?' parameter. I succeed to do it with one '?' but everytime I try to add the second parameter I got a MySQL Error

ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1

I don't know why the second '?' parameter throws me an error ...

Do I miss something ? Can anyone help me ? :)

1 Answer 1

1

From what I have seen elsewhere, the second parameter should be an array containing the query parameters. You are passing this:

[values]

but values is already an array. Can you try passing values directly:

connection.query(sql, values, (err, result, fields) => {
    console.log(sql);
    if (err) {
        throw err;
    };

    JSON.stringify(result);
    res.send({ movies: result });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that was my mistake ! It works perfect now !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.