user995691
user995691

Reputation: 89

Concatenate Mysql Query in PHP

im having problems concatenating this query:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId());

with this:

AND ticket.brand_id IN('.implode(',',$thisuser->getBrands())

how can i achive this?

Upvotes: 0

Views: 2232

Answers (1)

newfurniturey
newfurniturey

Reputation: 38416

You could concat the string on the same-line with:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId()) . ' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';

Or you could do it on a second line:

$sql.=' WHERE ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId());
$sql.=' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';

However, because your original WHERE clause contains an OR, you'll have to make sure to keep your logic intact. I would suggest to wrap the first two conditions in a single-set of parentheses before concatenating the additional AND clause:

$sql.=' WHERE (ticket.dept_id IN('.implode(',',$thisuser->getDepts()).') OR ticket.staff_id='.db_input($thisuser->getId()) . ')';
$sql.=' AND ticket.brand_id IN('.implode(',',$thisuser->getBrands()) . ')';

Upvotes: 1

Related Questions