Code : Tout sélectionner
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Rechercher</title>
</head>
<body>
<br>
<?php $connect=mysql_connect("localhost", "root", "") or die ("Echec de la connexion au serveur !");
$select=mysql_select_db("Gestion"); $query="select * from t_col";
$result=mysql_query($query); $totenreg=mysql_num_rows($result);
if ($totenreg==0) echo "<td colspan=9 align=center bgcolor='#000000'><h2>PAS DE COLIS A RECHERCHER</td>";
else if (!(empty($_POST["recherche"]))) {
echo "<table width=100% border=1 align=center>";
echo "<tr bgcolor='#000000'>";
echo "<td colspan=9><h2>RECHERCHER COLIS</td>";
echo "</tr>";
echo "<tr align=center bgcolor=#87CEEB>";
echo "<td><h4 class='h41'>Colis</td>";
echo "<td><h4 class='h41'>Nom</td>";
echo "<td><h4 class='h41'>Adresse</td>";
echo "<td><h4 class='h41'>CP</td>";
echo "<td><h4 class='h41'>Localité</td>";
echo "<td><h4 class='h41'>Envoi</td>";
echo "</tr>";
$i=0;
while ($row=mysql_fetch_array($result)) {
if (stristr($row[$_POST["choix"]], $_POST["recherche"]))
{ if ($i==0) { echo "<tr bgcolor=#D3D3D3>"; $i++; }
else { echo "<tr bgcolor=#FFFFFF>"; $i--; }
echo "<td><h5>".$row["NUMCOL"]."</td>";
echo "<td><h5>".$row["NOM"]."</td>";
echo "<td><h5>".$row["ADR"]."</td>";
echo "<td><h5>".$row["CP"]."</td>";
echo "<td><h5>".$row["LOC"]."</td>";
echo "<td><h5>".$row["ENV"]."</td>";
echo "</tr>"; } }
echo "</table>"; }
else
echo "<td colspan=9 align=center bgcolor='#000000'><h2>PAS DE COLIS A RECHERCHER";
?>
</body>
</html>
Code : Tout sélectionner
SELECT ... FROM ... WHERE code LIKE '%123A'Code : Tout sélectionner
SELECT ... FROM ... WHERE SUBSTRING(code, 7, 4) = '123A'
$sql = "SELECT ...
FROM t_col
WHERE ". $_POST["choix"] ." LIKE '%".$_POST["recherche"]."'"
Elle ne te renverra que les enregistrement pour lesquels la colonne $_POST["choix"] se termine par la chaine $_POST["recherche"].
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Rechercher</title>
</head>
<body>
<br>
<?php
if ( !$_POST[choix] || !$_POST[recherche]) {
echo "<td colspan=9 align=center bgcolor='#000000'><h2>SAISIR LES CRITERES DE RECHERCHE</td>";
exit;
}
$connect=mysql_connect("localhost", "root", "") or die ("Echec de la connexion au serveur !");
$select=mysql_select_db("Gestion"); $query="select * from t_col where $_POST[choix] LIKE '%$_POST[recherche]'";
$result=mysql_query($query);
if ( $result && mysql_num_rows($result) ==0)
echo "<td colspan=9 align=center bgcolor='#000000'><h2>PAS DE COLIS A RECHERCHER</td>";
else if ($result) {
echo "<table width=100% border=1 align=center>";
echo "<tr bgcolor='#000000'>";
echo "<td colspan=9><h2>RECHERCHER COLIS</td>";
echo "</tr>";
echo "<tr align=center bgcolor=#87CEEB>";
echo "<td><h4 class='h41'>Colis</td>";
echo "<td><h4 class='h41'>Nom</td>";
echo "<td><h4 class='h41'>Adresse</td>";
echo "<td><h4 class='h41'>CP</td>";
echo "<td><h4 class='h41'>Localité</td>";
echo "<td><h4 class='h41'>Envoi</td>";
echo "</tr>";
$i=0;
while ($row=mysql_fetch_array($result)) {
if ($i==0) { echo "<tr bgcolor=#D3D3D3>"; $i++; }
else { echo "<tr bgcolor=#FFFFFF>"; $i--; }
echo "<td><h5>".$row["NUMCOL"]."</td>";
echo "<td><h5>".$row["NOM"]."</td>";
echo "<td><h5>".$row["ADR"]."</td>";
echo "<td><h5>".$row["CP"]."</td>";
echo "<td><h5>".$row["LOC"]."</td>";
echo "<td><h5>".$row["ENV"]."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html><?php
$req="SELECT les_champs_voulus FROM latable WHERE RIGHT(`code`, 4) ='123A'";
?>
LIKE est lent, très lent.Mais l'avantage de Like est qu'on ne se limite pas aux 4 derniers mais aux N derniers (où N peut être toute la longueur de la chaine de recherche et ainsi permettre à la fois la recherche partielle que complète). La solution est extensible (réutilisable) alors sans mise à jour du code SQL.