如何在PHP中获取查询字符串变量


How to obtain a query string variable in PHP?

如何获取页面的id

我有30个链接的列表,它们看起来像:

http://test.com/composition.php?=9

这是我的代码:

(index.php)
    <?
    $q = array();
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $q[$row['id']]=$row['header'];
        }
    }
     ?>

 <?
       foreach($q as $href => $text) 
       {
           echo '<a href="http://test.com/composition.php?=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
       }
     ?>

当我点击链接时,如何在composition.php页面获得$href

我尝试了$_SESSION[href]=$href;,但它总是显示所有链接的最后一个id(=30),我需要我点击过的那个。

很抱歉没有问题,我是php的新手,不知道如何解决这个问题。

您需要为$href值创建一个密钥,以便使用$_GET数组访问它:

foreach($q as $href => $text) {
   echo '<a href="http://test.com/composition.php?id=' . $href . '">' .'<li>'. $text .'</li>' .'</a>';
}

然后在composition.php:

$href = isset($_GET['id']) ? $_GET['id'] : null;

查询值(传递到?后面的url中的值)是使用$_REQUEST超全局或$_GET超全局访问的键值对

更改

<a href="http://test.com/composition.php?=' . $href . '">

<a href="http://test.com/composition.php?get=' . $href . '">

并在php文件上获取id

$id = $_GET['get'];

欢迎