View Full Version : Get mysql result.

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:

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

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.

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.

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


$role = $row['role'];


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

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