Vue實現(xiàn)模糊查詢-Mysql數(shù)據(jù)庫數(shù)據(jù)
1.需求
輸入框中輸入數(shù)據(jù),根據(jù)輸入的結果模糊搜索數(shù)據(jù)庫對應內(nèi)容,實現(xiàn)模糊查詢。
2.實現(xiàn)
輸入框使用v-model
雙向綁定查詢數(shù)據(jù)keyWord
。
<el-input v-model="keyWord" placeholder="請輸入關鍵字搜索" clearable></el-input> <el-button type="success" icon="el-icon-search" @click="search"></el-button>
由于輸入框和顯示結果的不再同一view
下,所以在路由跳轉時將搜索結果傳遞給顯示結果的頁面,這里用的query
。
search函數(shù):
SearchResult.vue
代碼
在created
函數(shù)中獲取輸入框傳來的keyWord
getData(offset,limit)
函數(shù)使用axios
向后端根據(jù)keyWord
查詢數(shù)據(jù),其中offset
和limit
是分頁查詢的參數(shù)。
//請求數(shù)據(jù)庫數(shù)據(jù)的方法 getData(offset,limit){ this.axios.post('/php/search.php', qs.stringify({ offset: offset, limit: limit, keyWord: this.keyWord }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((res) => { this.total = res.data.total this.resultList = res.data.data }).catch((err) => { this.$message.error(err) })
獲取數(shù)據(jù)成功后就會將數(shù)據(jù)存入resultList
數(shù)組中,只需循環(huán)遍歷該數(shù)組就可以向前端展示查詢結果了。
后端使用的是php
寫的,主要利用了sql
語句的like
來實現(xiàn)模糊查詢。
后端search.php
文件,將數(shù)據(jù)庫連接基本信息改為自己的。
<?php $servername = "主機地址"; $username = "賬戶"; $password = "密碼"; $dbname = "數(shù)據(jù)庫名稱"; // 創(chuàng)建連接 $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } $keyWord = $_POST['keyWord']; //獲取前端的參數(shù) 開始和結束number if ( !isset( $_POST['offset'] ) ) { echo 0; exit(); }; $offset = ( int )$_POST['offset']; if ( !isset( $_POST['limit'] ) ) { echo 0; exit(); }; $limit = ( int )$_POST['limit']; //分頁查詢數(shù)據(jù)庫 $sql = "SELECT * FROM posts where title like '%$keyWord%' order by id desc LIMIT $limit OFFSET $offset"; $result = $conn->query($sql); $sqlGetCount = "SELECT COUNT(*) cnt FROM posts where title like '%$keyWord%'"; $rescnt = $conn->query($sqlGetCount); $rescnt = $rescnt->fetch_assoc(); $arr = array(); if ($result->num_rows > 0) { while ( $row = $result->fetch_assoc() ) { array_push( $arr, $row ); } //echo json_encode( $arr, JSON_UNESCAPED_UNICODE ); echo json_encode(array_merge(array('data'=>$arr),array('total'=>(int)$rescnt['cnt']))); } else { echo 0; } mysqli_close( $conn ); ?>
注意sql語句:
SELECT * FROM posts where title like '%$keyWord%' order by id desc LIMIT $limit OFFSET $offset;
like
后面應該使用 ’%$keyWord%‘
傳遞參數(shù),而不是 %’ $keyWord’%
,算踩了一個坑吧。
然后這是根據(jù)輸入的數(shù)據(jù)模糊查詢標題,也就是數(shù)據(jù)段title的,可以改為查詢其他的內(nèi)容。
3.結果
到此這篇關于基于Vue實現(xiàn)Mysql數(shù)據(jù)庫數(shù)據(jù)模糊查詢的文章就介紹到這了,更多相關Vue實現(xiàn)Mysql數(shù)據(jù)庫數(shù)據(jù)模糊查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue3中SetUp函數(shù)的參數(shù)props、context詳解
我們知道setup函數(shù)是組合API的核心入口函數(shù),下面這篇文章主要給大家介紹了關于Vue3中SetUp函數(shù)的參數(shù)props、context的相關資料,需要的朋友可以參考下2021-07-07在Vue中使用deep深度選擇器修改element UI組件的樣式
這篇文章主要介紹了在Vue中使用deep深度選擇器修改element UI組件的樣式,本文分為兩種方法給大家介紹,在這小編比較推薦使用第二種使用 deep 深度選擇器,感興趣的朋友跟隨小編一起看看吧2022-12-12vue3+electron12+dll開發(fā)客戶端配置詳解
本文將結合實例代碼,介紹vue3+electron12+dll客戶端配置,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-06-06一次在vue中使用post進行excel表下載的實戰(zhàn)記錄
最近遇到了需求,覺著有必要給大家總結下,這篇文章主要給大家介紹了關于一次在vue中使用post進行excel表下載的實戰(zhàn)記錄,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07