0

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';
  }
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
mysterykid
  • 93
  • 1
  • 9

5 Answers5

2

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';
  }
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • If you read the comments on the question, you'll note that $conn isn't a PDO object, which is what you linked from the docs. – Richard Theobald Jan 28 '16 at 16:31
  • @RichardTheobald I was going to make a mention of that too, but the principle is the same (the function is still generic). But Jay will rectify that, am sure ;-) – Funk Forty Niner Jan 28 '16 at 16:34
  • 1
    Thank you for your response/help! I still get the error: Cannot use object of type mysqli_result as array. So i shall ask my teacher about it tomorrow because there may be an error elsewhere. One again, thanks a lot! :) – mysterykid Jan 28 '16 at 16:56
0

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';
    }
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 28 '16 at 16:08
  • 1
    I second Jay's comment, stop using mysql_*. It's tea time ^__^ – yardie Jan 28 '16 at 16:10
  • The issue is not in your mysql_* functions. In main question $usertype = queryMysql("SELECT UserType FROM users WHERE Username='$user' AND Password='$pass'"); I will return a whole set of array. so you need to compare $usertype['UserType'] in the if statements. – Tayyab Ahmed Jan 28 '16 at 16:11
  • What is $usertype returning ? Use echo $usertype; – Tayyab Ahmed Jan 28 '16 at 16:22
  • @TayyabAhmed it currently returns an error: Cannot use object of type mysqli_result as array – mysterykid Jan 28 '16 at 16:24
  • 2
    Why should the OP try this? Why do you think it will solve the problem? A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Jan 28 '16 at 16:24
  • Use mysqli_fetch_assoc or mysqli_fetch_array to fetch a result row as an associative array. – Tayyab Ahmed Jan 28 '16 at 16:28
  • You still have code which will make the OP go backwards instead of moving on to newer API's which are safer and easier to use. – Jay Blanchard Jan 29 '16 at 16:47
0

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; 
}
0

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!

fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Thank you for your response/help and I understand what I have done wrong. However I still get the error: Cannot use object of type mysqli_result as array. Due to the echo strtolower part and the if statement as a whole. I will ask my teacher about it tomorrow :) – mysterykid Jan 28 '16 at 16:47
  • but have you modified the `queryMysql` function? last line `return $result->fetch_assoc();`, not `return $result` – fusion3k Jan 28 '16 at 17:05
0

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';
      }
mysterykid
  • 93
  • 1
  • 9