Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在mysql查询中使用foreach循环_Php_Foreach - Fatal编程技术网

Php 如何在mysql查询中使用foreach循环

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,

我想通过使用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, 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吗