Php 如果用户排名高于,则为else

Php 如果用户排名高于,则为else,php,mysql,Php,Mysql,我想做一个基于等级的导航系统。因此,用户的级别越高,他获得的导航选项就越多。我做到了: $rank = "SELECT rank FROM users WHERE username = '".$_SESSION['username']."'"; 然后我试着: if ($rank > 5) { // show rank 5 navigation } else { // show lower than rank 5 navigation } 但这对我没用 有什么想法吗?下面的例子应该足以

我想做一个基于等级的导航系统。因此,用户的级别越高,他获得的导航选项就越多。我做到了:

$rank = "SELECT rank FROM users WHERE username = '".$_SESSION['username']."'";
然后我试着:

if ($rank > 5) { 
// show rank 5 navigation
} else {
// show lower than rank 5 navigation
}
但这对我没用


有什么想法吗?

下面的例子应该足以让你走上正确的道路

有关从MySQL获取信息的最佳方法的详细信息,请访问

另外,看看MySQL和PHP的比较运算符

<?php

    // Place everything in a function to keep it somewhat organized.
    function displaySpecialUserMenu($username){

          // Connect to database
          $db = new PDO('mysql:host=localhost;dbname=testDatabaseName', 'testDatabaseUser', 'testPassword123');

          // Turn on error mode. Turn this OFF for production.
          $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

          // Select statement should return "1" if a user's rank is above 5
          $statement = $db->prepare('SELECT EXISTS(SELECT 1 FROM users WHERE rank > 5 AND username = :username');

          // Prepare the variables for the statement.
          $statement->bindParam(':username', $username, PDO::PARAM_STR);

          // Run the prepared statement
          $statement->execute();

          // Store the result (It'll be either a 0 or a 1) in $result
          $result = $statement->fetchColumn();

          if($result>0){
              // User's rank is greater than 5
              // Display menu here
              echo '[high_rank_menu_here]';
           }else{
              // User's rank is greater than 5
              // Display menu here
              echo '[low_rank_menu_here]';
           }


}

// Place this line where you want your menu to display.
displaySpecialUserMenu($_SESSION['username']);


?>

您还可以使用PHP而不是上面示例中的MySQL来执行逻辑

<?php

function displaySpecialUserMenu($username){

        // Connect to database
        $db = new PDO('mysql:host=localhost;dbname=testDatabaseName', 'testDatabaseUser', 'testPassword123');

        // Turn on error mode. Turn this OFF for production.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Select rank from user
        $statement = $db->prepare("SELECT rank FROM users WHERE username = :username");

        // Prepare the variables for the statement.
        $statement->bindParam(':username', $username, PDO::PARAM_STR);

        // Run the prepared statement
        $statement->execute();

        // Store the result
        $rank = $statement->fetchColumn();

        if($rank>5){
            // User's rank is greater than 5
            // Display menu here
            echo '[high_rank_menu_here]';
        }else{
              // User's rank is greater than 5
              // Display menu here
              echo '[low_rank_menu_here]';
        }
}

// Place this line where you want your menu to display.
displaySpecialUserMenu($_SESSION['username']);


?>


任何调试都会告诉您为什么这不起作用,研究也会告诉您为什么这是错误的。我建议您在询问有关堆栈溢出的问题之前,先执行这两项操作。你应该读一读,“但它对我不起作用”。方法您是否期望它能为您做一些烹饪工作?您是否甚至执行了SQL,或者您只是在一个字符串变量中定义它,希望奇迹会发生?您需要将其输入到PDO之类的数据库驱动程序中才能获得结果。看看如何解决这个问题。@Akhil不需要salty@DanBowell它看起来不像您所做的,因为保存SQL的同一个变量稍后会被比较,就好像它是一个结果集一样。