使用 URL 的一部分作为值从数据库获取数据 - PHP

Getting data from database using part of the URL as a value - PHP

我的url类型是“http://localhost/boardgame/profile/Duncan/”最后一个字是用户名...

我只是想从数据库中获取一些数据,我需要一个用户名来执行此操作。我试图从 URL 页获取它,但它不起作用。我哪里做错了?必须做什么?如果有人能提供帮助,我将不胜感激...

<?php


$actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

//echo $actual_link;

$rest = substr($actual_link, 35, -1);  

echo $rest;

$link = mysqli_connect("localhost", "root", "", "bgt");

if (mysqli_connect_error()) {

    die ("Veritabanı bağlantısında hata var");

} if (($rest) !="") {

    if (get_current_user_id($link) == '0') {

        echo "<p>Kullanıcının koleksiyonunu görebilmek için giriş yapmanız 
    gerekiyor.</p>";

          } else {

        $query = "SELECT TITLE FROM wp_user_collections WHERE `user_name` = 
   '".mysqli_real_escape_string($link, $rest)."'";

        echo "<div><h1>'".mysqli_real_escape_string($link, $rest)."'in 
Koleksiyonu</h1><div>";

        if($result = mysqli_query($link, $query)){

        if(mysqli_num_rows($result) > 0){

        echo "<table>";

        echo "<tr>";                
            echo "<th>Oyun Adı</th>";
        echo "</tr>";
            while($row = mysqli_fetch_array($result)){
        echo "<tr>";
            echo "<td>'" . $row['TITLE'] . "'</td>";
        echo "</tr>";

        }
        echo "</table></div>";
      }  
    }           
  }

} else {

echo "Kullanıcının koleksiyonunu görebilmek için <a 
href='http://localhost/boardgame/profile/'".mysqli_real_escape_string($link, 
$rest)."'>tıklayınız</a>";
}

?>

这是我想到的解决方案...使用数组然后提取最后一个元素(或倒数第二个,因为尾随的“/”斜杠是分隔符)

$uri="http://localhost/boardgame/profile/Duncan/";
$uriParts=explode('/', $uri);             //converts the uri string into an array of words separated by forward slash
$name=$uriParts[sizeOf($uriParts)-1-1];   //the user name comes from 2nd last item as the last item is empty
echo $name;

我可以建议你使用正则表达式来提取用户名,但我还不太熟悉它。