2

好的,所以我有一个 PHP 页面,我有一个数据库。在我的数据库中,我有一个带有字段的表,其中一个字段称为accounttype,它是enum('n', 'm', 's')如果用户是N , 我试图在我的 PHP 页面上显示它应该说普通用户,如果用户是E专家用户或S超级用户...

我该怎么做呢?

PHP 页面顶部

<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
    $phone = $row["phone"];
    $country = $row["country"];
    $state = $row["state"];
    $city = $row["city"];
    $accounttype = $row["accounttype"];
    $bio = $row["bio"];
}    
?>

我试图在页面上显示的位置 这是代码。现在它只是放置一个空白区域。

<span class="admin">Edward</span>
<span class="date">March 19, 2048</span>
<span class="tag"><?php echo "$accounttype"; ?></span>
<span class="comment"><a href="#">166 comments</a></span>

图片 http://i.stack.imgur.com/KXu9A.png

4

3 回答 3

1

首先建立一个连接,而不是不做一段时间,像这样做一个if

if($row = mysql_fetch_array($sql)){
    $phone = $row["phone"];
    $country = $row["country"];
    $state = $row["state"];
    $city = $row["city"];
    $accounttype = $row["accounttype"];
    $bio = $row["bio"];
}  

然后

$speaking_type = null;
switch($accounttype) {
    case 'n':
        $speaking_type = 'Normal User';
        break;
    case 'm':
        $speaking_type = 'Expert User';
        break;
    case 's':
        $speaking_type = 'Super User';
        break;
    default:
        $speagink_type = 'none';
        //throw new Exception('unsupported account type '.$accounttype);
}

echo $speaking_type;
于 2012-12-13T07:59:32.763 回答
0

I think the problem is your scope. Your variables are defined within the while-loop, and so they are unknown further in the document. Try instantiating them on top (before the while-loop) like this:

$phone = null;
$country = null;
$state = null;
$city = null;
$accounttype = null;
$bio = null;

Than the variables will be known outside the while and the values will be remembered when you print them.

于 2012-12-13T07:59:11.267 回答
0

I thought u didn't connect to the database first .use following code to the connect with your credentials.That's why you are seeing a blank space

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code

mysql_close($con);
于 2012-12-13T07:59:29.930 回答