简体   繁体   中英

How to get data from database using the letter search in PHP and MySQL

I need one help.I need to get data while user is typing letter inside text field from database using PHP and MySQL.I am explaining my code below.

$searchData=$_GET['search'] ;

In the above line user is getting the letter typed by user and once one letter will be typed the according to key word search the data will fetch from data base.I am explaining the table below.

db_name:

id    name     status

1      Raj      1    

2      Rahul     1   

3      Dog       1

4      Deer      1

Suppose user typed the the first letter R the all two names belongs to R should display and the single name will come when full name will be typed.Please help me to resolve this problem.

Try MySql fulltext search, it should do the job. here is a bit of code that i have used in one of my project.

    function getMemberIdByName($query)
    {
        //global mysql connection
        global $connection;

        $searchData = $query."*";

            $sql = "
                    SELECT 
                        * 
                    FROM 
                        member_table 
                    WHERE 
                        MATCH(name) AGAINST (:searchData IN BOOLEAN MODE) 
                    LIMIT 10
                    ";
            $statement = $connection->prepare($sql);
            $statement->bindValue(':searchData', $searchData);
            $statement->execute();
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            if (count($result) > 0) {
                return $result;
            } else {
                return null;
            }
    }

now use jquery ajax to call call this script with POST or GET value and call this function with the search value as a parameter.

you should be able to figure out the ajax part yourself.

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