简体   繁体   中英

Searching Data in Database. PHP

I've got table:

|-id-|--------name-------|

| 1  |  George Martin    |

I pull data like:

function search ($query)
{
    $query = trim($query);
    $query = mysql_real_escape_string($query);
    $query = htmlspecialchars($query);

    if (!empty($query))
    {
        if (strlen($query) < 3) {
            $text = '<p>Enter 3 or more letters.</p>';
        } else if (strlen($query) > 128) {
            $text = '<p>Too many letters.</p>';
        } else {
            $q = "SELECT *
                  FROM `table` WHERE `name` LIKE '%$query%'";

            $result = mysql_query($q);...

When I type in my search "George", It finds - "George Martin"

When I type in my search "Martin", It finds - "George Martin"

When I type in my search "Martin George", It doesn't find me anything.

Is there way to do last pull without making new table with name and second name ?

您的查询应为

SELECT * FROM `table` WHERE `name` LIKE '%$query%' or `name` ='$query'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM