0

Okay, here's the deal, I have a database let's say with 2 columns: "name" and "lastname"

When I type in a input box a name like "John" it would retrieve all John's from the "name" column. When I type in a lastname like "Doe" it would retrieve all Doe's from the "lastname" column. This works as expected.

But I would like it to also retrieve data from both columns when i type in "John Doe", so it would return all John Doe's. When typing one word in the searchbar, it works alright, but when i put in another word after space, it does not do what I want, coz I don't kno how should i form the query right.

Here's the code of query.php:

    <?php
    require_once("config.php");
    // Fetch the data
    $searchbar = $_POST['searchbar'];

    $querymilion = "SELECT * from lms.customers where `lastname` LIKE '%$searchbar%' OR `name` LIKE '%$searchbar%'";
    mysql_query('SET NAMES \'utf8\'');
    $resultmilion = @mysql_query($querymilion);

    // Return the results, loop through them and echo
    if($fraza) {
    while($rowmilion = mysql_fetch_array($resultmilion))
    {
    ?>
    <a href="javascript:klientid()" onClick="klientid()">
    <table id="tablesz" >
    <tr>
    <td style="font-weight:bold; width: 160px; text-align:right;">ID:</td>
    <td style="width:200px;" id="getidfromsearch"><?php echo $rowmilion['id']; ?></td>
    <td style="font-weight:bold; width: 50px; text-align:right;">PESEL:</td>
    <td style="width:200px;"><?php echo $rowmilion['ssn']; ?></td>
    </tr>
    <tr>
    <td style="font-weight:bold;width: 160px; text-align:right;">Nazwisko i Imię:</td>
    <td style="width:200px;"><?php echo $rowmilion['lastname']; ?> <?php echo   $rowmilion['name']; ?></td>
    <td style="font-weight:bold;width: 50px; text-align:right;">Dow.os.:</td>
    <td style="width:200px;"><?php echo $rowmilion['icn']; ?></td>
    </tr>
    <tr>
    <td style="font-weight:bold; width: 160px; text-align:right;">Adres instalacji</td>
    <td style="width:200px;"><?php echo $rowmilion['address']; ?></td>
    <td style="font-weight:bold; width: 50px; text-align:right;">Miasto:</td>
    <td style="width:200px;"><?php echo $rowmilion['zip']; ?> <?php echo $rowmilion['city']; ?>
    </td>
    </tr>
    </table>
    </a>
    <?php

    }
    }
    ?>

And here's the searchbar:

    <div id="szukaj">
    <table id="tablesz" >
    <tr>
<td class="naglowek">Szukaj klienta</td>
    </tr>
    <tr>
<td>
<form id="myForm" enctype="multipart/form-data" action="query.php" method="post"  name="myForm">
<input type="text" name="searchbar" value="" />
<input type="submit" value="Szukaj" /> 
</form>
</td>
    </tr>
    <tr>
<td>
<span id="error"></span>
<span id="result"></span>
</td>
    </tr>
    </table>
    </div>
4

2 に答える 2

0
SELECT * 
FROM  `actor` 
WHERE CONCAT( firstname,  ' ', lastname ) LIKE  "%$searchbar%"

テーブルが増えると、これは非常に遅くなる可能性があります。しかし、それは機能します。名姓の順序を姓名に切り替える可能性も必要な場合は、これをクエリに追加するだけです。

OR CONCAT( lastname,  ' ', firstname ) LIKE  "%$searchbar%"
于 2013-01-18T19:29:09.623 に答える
0

The issue is your CODE,

if you want to make a searh like John Doe you need to separate each word and put it into the lie, of course it will decrease the speed of the query.

I.E:

$querymilion = "SELECT * from lms.customers 
where (`lastname` LIKE '%$John%' OR `name` LIKE '%$John%')
and (`lastname` LIKE '%$Doe%' OR `name` LIKE '%$Doe%')";

this will give you the exact match with John Doe.

于 2013-01-18T19:06:36.387 に答える