2016-01-28 18 views
0

SQL是正确的,因为当我在phpMyAdmin中执行SQL时,它给了我正确的输出。下面的代码总是给我输出'指导员'。我该如何解决这个问题?我的if语句的输出总是相同的

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'; 
    } 
+2

[你的脚本是在对SQL注入攻击的风险。(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

+0

请使用PHP的[内置函数](http://jayblanchard.net/proper_password_hashing_with_PHP.html)来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用'password_hash()'[兼容包](https://github.com/ircmaxell/password_compat)。 –

+0

查询返回的结果是什么? –

回答

0

我设法得到它离开原来的MySQL()函数相同的(因为它是在别处另作他用),并简单地使用FETCH_ASSOC()此查询(我敢肯定有人在某处提到工作评论很抱歉) 感谢您的所有帮助! :)

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

我认为这会解决您的问题。

$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

请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+2

由于太多原因,这是错误的。不是我downvote顺便说一句。 –

+1

我第二个周杰伦的评论,停止使用mysql_ *。这是下午茶时间^ __^ – andre3wap

0

在您的代码中,$result是结果对象而不是实际结果。你需要做这样的事情:

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

由于$conn->query()神奇的你得到的结果回来从你的查询,你所要做的就是使用正确的格式来获取数据。一种方法是取一个关联数组,并返回:

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

$result现在是一个数组,其各部分可以通过提供标识符,您可以利用这样的优势,可以访问:

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

如果您阅读关于该问题的评论,您会注意到$ conn不是PDO对象,这是您从文档链接的内容。 –

+0

@RichardTheobald我也想提一提,但原理是一样的(该函数仍然是通用的)。但是周杰伦会纠正这种情况,当然;-) –

+1

谢谢你的回应/帮助!我仍然收到错误:不能使用mysqli_result类型的对象作为数组。所以我明天会问我的老师,因为其他地方可能会有错误。再一次,非常感谢!:) – mysterykid

0

您的代码失败,因为queryMysql()返回一个对象而不是预期的字符串。 正如评论所说,你必须改变你的函数以这种方式:

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

,那么你获得associative array与按键为您的表的列名。

所以,你可以以这种方式继续:

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

如意见建议,或者 - 如果你喜欢 - 你的方式:

​​

探索PHP Documentation(包括评论)你可以找到如何改善你的mySQL搜索。

...和编码玩得开心!

+0

感谢您的回复/帮助,我明白我做错了什么。但是我仍然得到错误:不能使用mysqli_result类型的对象作为数组。由于回声strtolower部分和if语句作为一个整体。我会问我的老师明天:) – mysterykid

+0

但你有没有修改'queryMysql'函数?最后一行'返回$ result-> fetch_assoc();',而不是'return $ result' – fusion3k