0

我有以下数组:

 $rqt="SELECT * FROM `client_gestion_compte` WHERE `id_company`='".$societe['id']."' AND `id_gestionnaire_client`='".$_SESSION['id']."'" ;
 $res=mysql_query($rqt);

 $values = array();
 while($donnees=mysql_fetch_assoc($res))
 {
     $values[] = $donnees['compte'] ;
 }

我想使用这个数组的值在数据库中进行搜索查询。我必须使用数组的所有值,但问题是我不知道数组中有多少项,所以我不能手动执行这个条件 LIKE $value 等等......

所以我试过这样。

`$sql="SELECT * FROM `debiteur` INNER JOIN `creance` ON `creance`.`n_doss` = `debiteur`.`n_doss` WHERE `creance`.`id_company`=".$societe['id']." AND `debiteur`.`nom` LIKE '%".$_GET['s']."%' AND `creance`.`code_client` LIKE '%".$values."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`prenom` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`raison_sociale` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`siret` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`adresse` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`adresse2` LIKE '%".$_GET['s']."%' Or `debiteur`.`ville` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`code_postal` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`tel` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`tel2` LIKE '%".$_GET['s']."%' Or `debiteur`.`fax` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`fax2` LIKE '%".$_GET['s']."%' Or `debiteur`.`gsm` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`gsm2` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`e_mail` LIKE '%".$_GET['s']."%' GROUP BY `creance`.`n_doss`  ";

问题出在这条线上 creance.code_client LIKE '%".$values."%'

因为我从不知道如何使用数组的所有自定义值进行搜索,而不仅仅是像 values[1] 这样的一个值。

4

3 回答 3

1

您需要使用foreach循环或implode遍历数组的所有元素,

$sql="SELECT * FROM `debiteur` INNER JOIN `creance` ON `creance`.`n_doss` = `debiteur`.`n_doss` WHERE `creance`.`id_company`=".$societe['id']." AND `debiteur`.`nom` LIKE '%".$_GET['s']."%' AND ";
 foreach($values as $v)  {
  $sql .= `creance`.`code_client` LIKE '%".$v."%' OR " ;
 }

 $sql .= " `creance`.`id_company`=".$societe['id']." AND `debiteur`.`prenom` LIKE '%".$_GET['s']."%' OR `creance`.`id_company`=".$societe['id']." AND `debiteur`.`.....";
于 2012-11-05T14:18:15.803 回答
1
$sql = "SELECT * FROM `debiteur` 
INNER JOIN `creance` ON `creance`.`n_doss` = `debiteur`.`n_doss` 
WHERE {...}
AND (`creance`.`code_client` LIKE '%" . implode("%' OR `creance`.`code_client` LIKE '%", $values) . "%')
{...}";
于 2012-11-05T14:31:39.120 回答
0
$rqt="SELECT * FROM `client_gestion_compte` WHERE `id_company`='".$societe['id']."' AND `id_gestionnaire_client`='".$_SESSION['id']."'" ;
 $res=mysql_query($rqt);

 $values = array();
 while($donnees=mysql_fetch_assoc($res))
 {
     $values[] = $donnees['compte'] ;
 }

 $value_query=implode(",",$values);

 //Place it like this in your query

 creance.code_client IN ('".$value_query."')
于 2012-11-05T14:29:01.743 回答