Help about sql query

Hello,
This syntax does not work in group

$result = $mysqli->query("SELECT * FROM users WHERE group IN ( 1,2 ) AND
column_a LIKE '$search' OR
column_b LIKE '$search' OR
column_c LIKE '$search' OR
column_d LIKE '$search' OR ORDER BY id ASC LIMIT ".$start_from.", ".$limit);

Thank you from now

You cut and pasted too much. The last OR does not belong.

always enable error reporting on your development machine

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
1 Like

I used it, saw other errors and corrected
Thank you

I fixed it, but again “group IN (1,2)” doesn’t work

$result = $mysqli->query("SELECT * FROM users WHERE group IN ( 1,2 ) AND
column_a LIKE '$search' OR
column_b LIKE '$search' OR
column_c LIKE '$search' OR
column_d LIKE '$search' ORDER BY id ASC LIMIT ".$start_from.", ".$limit);

You didn’t state in what way it doesn’t work.

However, the answer is probably - operator precedence. The query is ANDing the GROUP IN(1,2) term with the 1st LIKE term, then ORing that with the rest of the LIKE terms. If you want all the LIKE terms to ORed together, you need to add ( ) around the ORed terms.

Next, don’t put external/unknown data directly into an sql query statement, Use a prepared query with a place-holder for each value, then supply the values when the query is executed.

Also, since you appear to be using pagination, you should be building the common part of the query, with any FROM, JOIN, WHERE, GROUP BY, and HAVING terms in a php variable,then use this common part of the query in both the COUNT() query and the data retrieval query.

And, since GROUP is a reserved word, either that’s not the real column name or you should be getting a sql syntax error.

And, list out the columns you are selecting, instead of using *.

1 Like

User List
1,2,3 from user group
I want only 1 and 2 groups listed in the query
also have user search feature in user list

I enclosed search (variables) in parentheses and problem improved

Sponsor our Newsletter | Privacy Policy | Terms of Service