如何获取表行的值并在else if语句中使用它

时间:2014-01-25 01:49:51

标签: php mysql sql

我不擅长从数据库中选择信息。

我想为买家和卖家提供不同的标题。我检查他们是否已登录但不知道如何让代码查看数据库并告诉他们是否以买家或卖家身份登录。

这就是我所拥有的:

if(isset($_SESSION['logged_in']) == 1 && (check if usertype is buyer and maybe put this into a variable)) { include('header_buyer.php');

}

else (isset($_SESSION['logged_in']) == 1 && (check if usertype is seller)) { include('header_seller.php'); 

}

如果他们是买家或卖家,我不知道如何从我的表'users'中查看usertype,并且可能将其放入变量中,因此可以将其写为

if(isset($_SESSION['logged_in']) == 1 && ($usertype == 'buyer')) { include('header_buyer.php');

......或类似的东西。

2 个答案:

答案 0 :(得分:0)

从数据库中选择您的用户,然后在会话中记录用户类型

mysql_connect("localhost", "username", "password") or die ("Can't connect to database server");
mysql_select_db("yourDb") or die ("Can't select database");
$query = mysql_query('select user_id, user_type from user user_id = 2');

$resutl = mysql_fetch_array($query);

if ($resutl) {
    $_SESSION['user_type'] = $resutl['user_type'];
    $_SESSION['logged_in'] = 1;

}

答案 1 :(得分:0)

我建议......

// connect to the database
mysql_connect('localhost','username','password') or die ('Can\'t connect to the database');
mysql_select_db('yourDatabase') or die ('Can\'t select database table');

// check the session to see if they are logged in or not
if (isset($_SESSION['logged_in'])) {

    // we know the user is logged in, so query the database work out what type they are
    $query = mysql_query('SELECT `userType` FROM `yourTable` WHERE `id` = theirID LIMIT 1;')

    // extract the data from the result
    $userType = mysql_result($query,0,0); // row 0, column 0 of the return data

    // include the file you need
    if ($userType=='buyer') {
        include('header_buyer.php');
    } elseif ($userType=='seller') {
        include('header_seller.php');
    } else {
        // handle an invalid user type
    }

} else {

    // they are not logged in

}

当然,您需要更改数据库字段,并在其存储的任何位置包含其ID(如果未存储,则可以在登录过程中将其存储在会话中)。