使用 PHP 从 MySQL 数据库中获取单个值


Get a single value from MySQL database using PHP

我需要在Joomla站点中获取当前页面的访问级别。我能够使用以下方法获取页面 ID:

/* Define $jinput */
$jinput = JFactory::getApplication()->input;
/* Get the current page id */
$page= $jinput->get('id');

现在,我想查询数据库以返回当前页面的访问级别。表内容如下所示:

page_id | access | ...
1234    | 10     |...

因此,第 1234 页的访问 ID 为 10。这是我试图得到的 10 个:/* 打开连接 */

$link=mysqli_connect('localhost','user','pass');
    if(!$link){
    echo "No connection";
    exit();
    }
    if (!mysqli_set_charset($link, 'utf8'))
    {
      echo 'Unable to set database connection encoding.';
      exit();
    }
    if(!mysqli_select_db($link, 'datab')){
        echo "Can't find database";
    exit();
};
/* Find the access level of the current page */
$query = "SELECT access FROM content WHERE id=$page";
try {
    $result=$link->query($query);   
}
catch (PDOException $e){
    $error="Error".$e->getMessage();
    exit();
}
$row=mysql_fetch_array($result);

从页面获取单个值似乎非常复杂,但它不起作用!我知道查询是正确的,因为我在我的管理员 PHP 中测试了它。

请帮忙,谢谢!

我认为这就是您可能正在寻找的:

$app = JFactory::getApplication();
$access = $app->getMenu()->getActive()->access;
echo "Access level = " . $access;