...

View Full Version : Get mysql result.



greenhat
10-19-2012, 08:17 PM
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:


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?

Redcoder
10-19-2012, 10:29 PM
Where do you want to set the switch and which variables/strings do you want to run through it? Expound on that.

greenhat
10-20-2012, 12:27 AM
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.

Redcoder
10-20-2012, 12:37 AM
Well, for the role field:



<?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 (http://php.net/manual/en/control-structures.switch.php).

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum