Php 如何在mysql查询中使用foreach循环
我想通过使用foreach循环向sqlselect添加一些条件。例如:Php 如何在mysql查询中使用foreach循环,php,foreach,Php,Foreach,我想通过使用foreach循环向sqlselect添加一些条件。例如: select id, custom_id, name, customer from db_clients where id = 5 通过使用foreach,我想了解更多的“where”条件 select id, custom_id, name, customer from db_clients where id = 5 and id = 19 我的PHP代码: $this ='select id, custom_id,
select id, custom_id, name, customer from db_clients where id = 5
通过使用foreach,我想了解更多的“where”条件
select id, custom_id, name, customer from db_clients where id = 5 and id = 19
我的PHP代码:
$this ='select id, custom_id, name, customer from db_clients where id = 5' . foreach($ids AS $id){
'. and id = '.$id}';
问题在哪里
$this ='select id, custom_id, name, customer from db_clients where id in ('.implode(',',$ids).') ';
假设$ids始终至少有一个数组值,它可以是空的。您需要在执行此操作之前添加另一个检查,或者在填充数组之前添加一个-1或一些永远不存在的id,以确保它永远不为空。有很多方法可以处理空的情况。您可以使用添加另一个条件,这就是您的意思吗?我想要“和”我现在看到的循环中的条件有很多次,你不能先在php中内爆$ids吗