Php 操作MySQL数据库数据

Php 操作MySQL数据库数据,php,mysql,Php,Mysql,我有一个MySQL数据库,它在列中保存一个类列表,每个类都有一个“level”,它是行的值,每个人的值都会改变。例如,math有0、1、2和3个可能的值,0没有被选为类,1、2和3分别为高、中、低 我有一个MySQL查询,它只从用户的数据库行中提取类 $result = mysql_query("SELECT math, physics, biology, chemistry, english, spanish, history, economics, art, theoryofknowledg

我有一个MySQL数据库,它在列中保存一个类列表,每个类都有一个“level”,它是行的值,每个人的值都会改变。例如,math有0、1、2和3个可能的值,0没有被选为类,1、2和3分别为高、中、低

我有一个MySQL查询,它只从用户的数据库行中提取类

$result = mysql_query("SELECT math, physics, biology, chemistry, english, spanish, history, economics, art, theoryofknowledge, extendedessay FROM users WHERE username = '". $login_session ."'") or die(mysql_error());  

        while($row = mysql_fetch_array( $result )) {
            echo "Math:". $row['math'] ."<br />";
            echo "Physics:". $row['physics'] ."<br />";
            echo "Biology:". $row['biology'] ."<br />";
            echo "Chemistry:". $row['chemistry'] ."<br />";
            echo "English:". $row['english'] ."<br />";
            echo "Spanish:". $row['spanish'] ."<br />";
            echo "History:". $row['history'] ."<br />";
            echo "Economics:". $row['economics'] ."<br />";
            echo "Art:". $row['art'] ."<br />";
            echo "Theory of Knowledge:". $row['theoryofknowledge'] ."<br />";
            echo "Extended Essay:". $row['extendedessay'];
        }
如果每次调用类时都不执行if语句,我如何确定用户的级别?我需要在网站上的多个地方调用这些类,并且我想要一种简单的方法来检查用户拥有哪些类和级别


谢谢

我认为最好的方法是构建一个PHP类,将用户加载到该类中,并在其中包含一个可以接受类(生物学、数学等)并返回用户级别的函数。如果您愿意,您可以编写一些简单的代码来执行所需级别的检查,并根据用户的级别是否足够高返回true或false

我甚至拼凑了一些你可能想要扩展的超基本结构:

<?php

    class myUser
    // You are making an object here that stores information about your user.
    // This will mean you only need to query that data once from the DB, then
    // you can use it anywhere on the page without needing to do more queries.
    {
        public $math;
        public $biology;
        // I am making public variables here based on your columns
        // You cuold just as easily make an array for example to store them in.

        public function __construct($userID)
        // Making a construct class - meaning you will be able to write a snippet
        // like this: $currentUser = new myUser(6);
        // and the user information will be loaded nicely for you
        {
            $query="select math, biology from users where ID=$userID";
            // database stuff ....
            // this is where you would write your actual code to get the info
            // from the database and populate it properly, not like I did
            // below for this example
            $this->math=4;
            $this->biology=2;
        }

        public function checkUserLevel($myTopic, $reqLevel)
        // Making use of a few things here that I should ntoe:
        // This is a function you can call from the main code below
        // like this: $currentUser->checkUserLevel('math',3)
        // it will return either true or false.
        // I have used variable variables here for the $myTopic to
        // make it easier. You normally access an element differently
        // normally it is like: echo $this->math;  // output 4
        // Also I am using a ternary operator to return the data,
        // which is just a shortcut.
        {
            return ($this->$myTopic>=$reqLevel)? true : false;
        }

        public function returnUserLevel($myTopic)
        {
            return $this->$myTopic;
        }

    }

    $currentUser = new myUser(6);
    // This is creating a new user object based on the class we made above.
    // Further Explanation:
    // We have a class called myUser, but a class is just a schematic.
    // The $currenUser bit defines a new variable in our code.
    // the "= new myUser" bit says that we want to use the schematic above for this variable
    // the "myUser(6)" basically gives the constructor function an ID to get from
    // the database for the user. Because we defined a constructor class that expects
    // an ID, we need to supply it one, else we will get an error.
    // So $currentUser = new myUser(6)
    // really means "Make me a new variable called $currentUser and make it of the myUser
    // class type, and when making it, fetch me the details of student ID 6 and populate it
    // with their data.

    if($currentUser->checkUserLevel('math',3))
    // Now I am using one of the functions called checkUserLevel, supplying it
    // with the two arguments it needs and depending on it it returns true or false
    // doing one action or another.
    {
        echo "The user is at least level 3.\n";
    }
    else
    {
        echo "The user is lower than level 3.\n";
    }

    if($currentUser->checkUserLevel('biology',12))
    // same check, differnt inputs
    {
        echo "The user is at least level 3.\n";
    }
    else
    {
        echo "The user is lower than level 3.\n";
    }


    // You can output like this for example:
    echo "The users math is at: ".$currentUser->math;

    // and I added a little function that will simply return the level for you of the subject you enter.
    echo "The user is at math level ".$currentUser->returnUserLevel('math');

    // lastly you can do something like this:
    $allSubjects=array('math','physics','biology');
    for($i=0;$i<count($allSubjects);$i++)
    {
        echo "The users ".$allSubjects[$i]." level is at ".$currentUser->returnUserLevel($allSubjects[$i])."<br><br>";
    }

?>

一种方法是在用户登录时将所有类的值存储在PHP会话变量中:

<?php
    session_start();
    $_SESSION["classes"] = array();       

    $row = mysql_fetch_array($result);
    foreach($row as $class => $level)
    {
        $_SESSION["classes"][$class] = $level;
    }
?>

然后,您可以在站点的任何页面上访问用户的类级别,直到他们关闭会话或注销:

<?php
    session_start();
    $mathLevel = $_SESSION["classes"]["math"];
?>


这个问题太模糊了,无法回答。@IgnacioVazquez Abrams你介意看看我的答案吗?我想你可能错过了一个很好的机会,让一个程序员同事开始清理OOP PHP代码。我是PHP新手,请你详细说明一下好吗?@AlexCastro是的,我刚刚为你添加了一个带有示例代码的编辑:)@AlexCastro添加了大量注释来解释代码中发生了什么。@AlexCastro好的,在
$currentUser=newmyuser(6)周围添加了一堆更多的注释您的代码行。$currentUser=new myUser(6);我是否需要一次又一次地复制这一行以进行不同的检查,因为这只包含一个变量?
<?php
    session_start();
    $_SESSION["classes"] = array();       

    $row = mysql_fetch_array($result);
    foreach($row as $class => $level)
    {
        $_SESSION["classes"][$class] = $level;
    }
?>
<?php
    session_start();
    $mathLevel = $_SESSION["classes"]["math"];
?>