亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 GET 請求中解析數據庫表

從 GET 請求中解析數據庫表

PHP
蕭十郎 2022-12-23 16:27:18
早上好,我最近一直在努力解決這個問題,因為我對 PHP 和 MySQL 還很陌生。我有一個帶有“視頻”表的數據庫,我在其中存儲了有關視頻的有用信息,并且我有一個名為 search.php 的文檔,該文檔將根據 GET 請求顯示特定視頻。請求看起來像這樣:http://example.ex/search.php?tag=EXAMPLE1邏輯是像這樣存儲標簽值:if(!empty($_GET["tag"])){     // Get videos from tag only     $curTag = strval($_GET["tag"]);     displayByTag($curTag); //the function that parse the database}我已準備好連接:$server = "localhost";$username = "root";$password = "";$db = "mydatabase";$conn = mysqli_connect($server, $username, $password, $db);$query = "SELECT * FROM videos";$response = array();$result = mysqli_query($conn, $query);while($row = mysqli_fetch_array($result)) {     $response[] = $row;}從技術上講,截至目前,我的表存儲在里面$response[].我需要做的是解析數據庫并查找“標簽”列,拆分其字符串值(表中的“EXAMPLE1,EXAMPLE2,EXAMPLE3”),然后查看是否GET 值匹配其中之一。那是我需要你幫助的時候。我了解邏輯和步驟,但無法將其“翻譯”成 PHP。這是我會做的(人類語言):function displayByTag($tag) {     for each $video-item inside $array {          $tagsArray = explodes(",", $video-item[tags-column]); //That's how I split the tags stored inside the table          for i as integer = 0 to $tagsArray.length {               if $tagsArray(i) == $tag {                    //THATS A MATCH               }          }     }}這是正確的方法嗎?我怎樣才能將這種“人類”語言翻譯成 PHP 代碼?謝謝您的幫助。
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

經過一些測試和調試后,我的功能很容易運行。如果有人感興趣:


function searchVideos($search) {

    $currentSearchQueries = explode(" ", strtoupper($search)); //Split the searched tags in a array and make them to uppercase for easier comparaison.


    //Establish a connection the MySql Database

    $server = "localhost";

    $username = "root";

    $password = "";

    $db = "mydatabase";

    $conn = mysqli_connect($server, $username, $password, $db);


    //Select all the entries from my 'videos' table

    $query = "SELECT * FROM videos";

    $response = array();

    $result = mysqli_query($conn, $query);

    while($row = mysqli_fetch_array($result)){

        $response[] = $row; //Place them into a array

    }


    //Parse the array for matching entries

    foreach ($response as &$video){ //Each entries goes through the process

        foreach ($currentSearchQueries as $t) {

            //We compare if one the tags searched matches for this particular entry

            if((strtoupper($video[tags]) == $t) {

                //THAT'S A MATCH

            }

        }

    }

}

編碼很有趣,期待新的體驗!


查看完整回答
反對 回復 2022-12-23
  • 1 回答
  • 0 關注
  • 78 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號