jquery select2: error in getting data from php-mysql

后端 未结 2 1930
再見小時候
再見小時候 2020-12-10 09:19

I am testing select2 plugin in my local machine. But for some reason. it is not collecting the data from database.

I tried multiple times but not

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 09:49

    Here is the answer. how to get the data from database.

    tag.php

    
    

    fetch.php

    prepare("SELECT tid,tag FROM tag WHERE tag LIKE :search LIMIT 4");
    
    // Add a wildcard search to the search variable
    $query->execute(array(':search'=>"%".$search."%"));
    
    // Do a quick fetchall on the results
    $list = $query->fetchall(PDO::FETCH_ASSOC);
    
    // Make sure we have a result
    if(count($list) > 0){
       foreach ($list as $key => $value) {
        $data[] = array('id' => $value['tag'], 'text' => $value['tag']);            
       } 
    } else {
       $data[] = array('id' => '0', 'text' => 'No Products Found');
    }
    
    
    // return the result in json
    echo json_encode($data);
    
    ?>
    

    With the above code i am able to get the data from database. I get help from multiple users from SO. Thanks to all of them.

    However, i am still refining other areas like adding tag in database. Once it completed i will post full n final code.

提交回复
热议问题