mysterykid
mysterykid

Reputation: 133

The output of my if statement is always the same

The SQL is correct because when I execute the SQL in phpMyAdmin it gives me the correct output. The code below always gives me the output 'instructor.' How can I solve this??

queryMysql($query) { 
    global $conn; 
    $result = $conn->query($query); 
    if (!$result) { 
        die($conn->error); 
    } 
    return $result; 
}



  $usertype = queryMysql("SELECT UserType FROM users WHERE Username='$user' AND Password='$pass'");
  $student = 'Student';
  $teacher = 'Teacher';
  $instructor = 'Instructor';
  if ($usertype === $student){
      echo 'student';
  } elseif ($usertype === $teacher) {
      echo 'teacher';
  } else {
      echo 'instructor';
  }

Upvotes: 0

Views: 104

Answers (5)

mysterykid
mysterykid

Reputation: 133

I managed to get it to work by leaving the original mysql() function the same (as it is used elsewhere for another purpose) and simply using fetch_assoc() for this query (which i'm sure someone mentioned somewhere in the comments so sorry) Thanks to all of your for your help!! :)

      $usertype = queryMysql("SELECT UserType FROM users WHERE Username='$user' AND Password='$pass'");
      $result2 = $usertype->fetch_assoc();
      $student = 'Student';
      $teacher = 'Teacher';
      $instructor = 'Instructor';
      if ($result2['UserType'] === $student){
      echo 'student';
      } elseif ($result2['UserType'] === $teacher) {
      echo 'teacher';
      } else {
      echo 'instructor';
      }

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34426

Because of the magic of $conn->query() you're getting the results back from your query, all you have to do is use the right format to get the data. One way is to fetch an associative array and return that:

queryMysql($query) { 
    global $conn; 
    $result = $conn->query($query); 
    if (!$result) { 
        die($conn->error); 
    } 
    return $result->fetch_assoc(); 
}

$result is now an array and its parts can be accessed by providing identifiers, which you can take advantage of like this:

 if ($usertype['UserType'] === $student){
      echo 'student';
  } elseif ($usertype['UserType'] === $teacher) {
      echo 'teacher';
  } else {
      echo 'instructor';
  }

Upvotes: 2

fusion3k
fusion3k

Reputation: 11689

Your code fails because queryMysql() return an object instead of expected string. As suggested in comments, you have to change your function in this way:

function queryMysql( $query ) 
{ 
    (...) 
    return $result->fetch_assoc();
}

then you obtain an associative array with keys as your table column names.

So, you can continue in this way:

$usertype = queryMysql("SELECT UserType FROM users WHERE Username='$user' AND Password='$pass'");
echo strtolower($usertype['UserType']);

as suggested in the comments, or - if you prefer - in your way:

$student = 'Student';
(...)
if( $usertype['UserType'] === $student ) {
(...)
}

Exploring the PHP Documentation (including comments) you can found how improve your mySQL searches.

... and have fun with coding!

Upvotes: 0

Richard Theobald
Richard Theobald

Reputation: 777

In your code, $result is a result object rather than the actual results. You need to do something like this:

queryMysql($query) { 
    global $conn; 
    $dbQuery = $conn->query($query);
    $result = $dbQuery->fetch_assoc(); //this gets the actual row rather than just the mysqli_result object
    if (!$result) { 
        die($conn->error); 
    } 
    return $result; 
}

Upvotes: 0

Tayyab Ahmed
Tayyab Ahmed

Reputation: 83

I think it will solve your problem.

    $result = mysql_query("SELECT UserType FROM users WHERE Username='$user' AND Password='$pass'");
    $usertype = mysql_fetch_array($result);
    $student = 'Student';
    $teacher = 'Teacher';
    $instructor = 'Instructor';
    if ($usertype['UserType'] == $student){
      echo 'student';
    } elseif ($usertype['UserType'] == $teacher) {
      echo 'teacher';
    } else {
      echo 'instructor';
    }

Upvotes: 0

Related Questions