从GET请求解析数据库表

时间:2020-04-16 11:55:57

标签: php mysql database parsing request

早上好
我最近一直在努力工作,因为一般来说我对PHP和MySQL还是很陌生。 我有一个带有“视频”表的数据库,其中存储了有关视频的有用信息,还有一个名为search.php的文档,它将根据GET请求显示特定的视频。 请求看起来像这样:

npm install react-native-screens
react-native link react-native-screens

逻辑将是像这样存储标记值:

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; }
我需要做的是解析数据库并查找“标签”列,拆分其字符串值(表中的“ EXAMPLE1,EXAMPLE2,EXAMPLE3”),然后查看GET值是否与其中之一匹配。

那是我需要您帮助的时候。我了解逻辑,步骤,但无法将其“转换”为PHP。这是我会做的(人工语言):

$response[].

这是正确的方法吗?以及如何将“人类”语言转换为PHP代码?
感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

经过一些测试和调试,我的功能很容易运行。如果有人感兴趣:

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
            }
        }
    }
}

编写代码非常有趣,期待新的体验!