2

Ok So i have an PHP page and i have a database. In my database i have a table with a Field one of the fields is called accounttype it is enum('n', 'm', 's') I am trying to display on my PHP page if the user is N it should say Normal User if the user is E Expert user or S Super user...

How do i go about doing this?

Top of PHP Page

<?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"];
}    
?>

Where i am trying to display on the page This is the code. Right now it just puts a blank space.

<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>

picture https://i.sstatic.net/KXu9A.png

4
  • 1
    This is not the cause of the issue, but the quotes around the $accounttype variable are unnecessary! Commented Dec 13, 2012 at 7:47
  • Read the last post in this link it might be what you are looking for 1-script.com/forums/php/… Commented Dec 13, 2012 at 7:57
  • How do you display the HTML? Is it inside or outside the loop? Will be easier for us if you put your complete code and maybe your database row values. Commented Dec 13, 2012 at 8:17
  • You should really select an answer if your problem is solved. Commented Jun 27, 2013 at 22:46

3 Answers 3

1

first make a connection, than dont make a while, make a if like this

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

and than

$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;
Sign up to request clarification or add additional context in comments.

2 Comments

I am now getting this error Fatal error: Uncaught exception 'Exception' with message 'unsupported account type ' in /home/content/31/7044931/html/member/member_account.php:86 Stack trace: #0 {main} thrown in /home/content/31/7044931/html/member/member_account.php on line 86
@AlexMoneyyAmk ok your field is nullable... i changed my code... check out
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.

1 Comment

That won't work. It doesn't know what rows to get it would leave all the information blank because you are not defining what row is what.
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);

1 Comment

i am connected to my database :) all the other information is passing threw except the accounttype. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.