我有以下数组:
$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."%'
因为我从来不知道如何使用数组的所有自定义值进行搜索,而不仅仅知道一个值,例如值[1]。
答案 0 :(得分: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`.`.....";
答案 1 :(得分: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) . "%')
{...}";
答案 2 :(得分: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."')