我需要执行一个mysql查询,它将搜索(在列中)nome
和cognome
(在Prenotazioni
表中)。
问题是:查询必须搜索名称和姓氏或姓氏和名称(它们也可以是3或4个值),我只有1个搜索表单。如果我有两个不同姓氏的约翰(黑色和白色),并且我将搜索表单放入约翰黑色或白色约翰,查询将给我两个结果
我如何解决它?
的index.php
<table border="10">
<tr>
<td align="center"><b>CERCA</b></td>
</tr>
<tr>
<td>
<table>
<form method="post" action="./cerca.php">
<tr>
<td>Cerca:</td>
<td width="250" align="left"><input type="text" autocomplete="off" size="70" name="ricerca"/>
</td>
</tr>
<tr>
<td></td>
<td align="left"><input style="width:100px;" type="submit" value="Cerca"/>
</tr>
</form>
</table>
</td>
</tr>
</table>
cerca.php
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>Ricerca</title>
</head>
<body>
<?php
include('./config.php');
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$q = 'SELECT * FROM Prenotazioni WHERE 1=2';
$parts = explode(' ', $name);
foreach ($parts as $part) {
$q .= ' OR nome LIKE \'%' . $part . '\' OR cognome LIKE \'%' . $part . '\'';
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<td width="242">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td> <p><br>
<br>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}
?>
答案 0 :(得分:1)
我在3天前遇到了同样的挑战,这解决了它:将cerca.php的第6-11行修改为此
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
鉴于&#34; john black &#34;,&#34; black john &#34;和&#34; bla joh &#34;返回相同的结果和&#34;白约翰&#34;与&#34;黑约翰&#34;相同。我希望它可以帮到你
答案 1 :(得分:0)
使用上面的mu代码创建mysql查询字符串并运行此查询并根据您的行生成
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i) {
$q .= ' and ';
}
$q = $q . " (concat(nome, ' ', cognome) LIKE '%'.$parts[$i].'%')";
}
答案 2 :(得分:-1)
修改强> 好的代码:
$name = isset($_POST['ricerca']) ? preg_replace('/\s+/', ' ', trim($_POST['ricerca'])) : '';
if ($name != '') {
$parts = explode(" ",$name);
$count = count($parts);
$q = "SELECT * FROM Prenotazioni WHERE";
for($i = 0;$i < $count;$i++)
{
if($i != $count-1)
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%') AND ";
else
$q = $q.
" (nome LIKE '%".$parts[$i]."%' OR
cognome LIKE '%".$parts[$i]."%')";
}
$exec = mysql_query($q);
if (mysql_num_rows($exec)) {
echo '<tr>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr></tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Nome</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Cognome</strong></td>
</tr>';
while($row = mysql_fetch_array($exec)) {
echo '<tr>
<td align="center" bgcolor="#FFFFFF">' . $row['nome'] . '</td>
<td align="center" bgcolor="#FFFFFF">' . $row['cognome'] . '</td>';
}
}
else {
echo 'Errore: nessuna corrispondenza trovata.';
}
}
else {
echo 'Errore: Inserisci un nome.';
}