Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Get mysql result.

    This may be the most simple thing to do in php. Forgive me, I am from the .net world where all this is simple. all I want is what this query is:
    PHP Code:
                    session_start();
                    include 
    "connectTo.php";
                    
    $link connectTo();
                
    $id $_SESSION['email'];
             
    $table "users";
                    
    $query =  "SELECT role FROM $table WHERE username ='$id'";
                    
    $result mysql_query($query,$link);
                    if (!
    $result) {
                    
    $message  'Invalid query: ' mysql_error() . "\n";
                    
    $message .= 'Whole query: ' $query;
                     die(
    $message);
                    }
                    while (
    $row mysql_fetch_assoc($result)) {
                    echo 
    $row['username'];
                   } 
    how do i echo or get this comparison so I can set up a switch on the result?
    Last edited by greenhat; 10-19-2012 at 08:58 PM.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    334
    Thanks
    2
    Thanked 48 Times in 47 Posts
    Where do you want to set the switch and which variables/strings do you want to run through it? Expound on that.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    10
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Get mysql result.

    I have a string field in the users table called role. I want to show different forms based on five different role types. The code will get quite lengthy. Rather use a switch than a bunch of if statements. So i want the query result to spit out a string to compare.

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    334
    Thanks
    2
    Thanked 48 Times in 47 Posts
    Well, for the role field:

    PHP Code:
    <?php

        $role 
    $row['role'];

    switch(
    $role){

         case 
    'actor':
          
    //Actor code
              
    break;
     case 
    'janitor':
          
    //janitor code
              
    break;
          
    //Blah blah as many cases as you want

    default:
    //Code to be executed if $role does not fall into any of the above

    }

    ?>
    Check out more about switches here.

    I'm guessing that maybe the problem is the flow of logic from the while statement to the switch statement. If so, don't hesitate to post it here.
    Last edited by Redcoder; 10-20-2012 at 06:14 PM.

  • Users who have thanked Redcoder for this post:

    greenhat (10-20-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •