简体   繁体   English

PHP if语句与MySQL查询

[英]PHP if statement with a MySQL query

Is there a way to do an if statement like this: 有没有办法做这样的if语句:

if ($password == mysql_query("SELECT password from member WHERE id = $id")) {

// f00

}

So what I mean is, if it is possible and/or good practice to have a short query in your if , instead of extracting it from your DB first. 所以我的意思是,如果可能和/或好的做法是在if进行简短查询,而不是先从数据库中提取出来。

Thanks! 谢谢!

mysql_query返回资源,因此它将无法工作

I wouldn't recommend it. 我不推荐它。 If there is an error in the SQL, if you can't connect to the server, if there are no rows returned, or if there are any other errors then you'd have some big problems. 如果SQL中有错误,如果您无法连接到服务器,如果没有返回行,或者有任何其他错误,那么您将遇到一些大问题。

Sure it's possible but it's not a good idea. 当然有可能,但这不是一个好主意。

Consider if you were using a database abstraction layer instead of basic php_mysql like adodb 考虑是否使用数据库抽象层而不是像adodb这样的基本php_mysql

you could easily have: 您可以轻松拥有:

   if($password == $DB->GetOne('Select password from member where id = '.$ID)){
        //do something
   }

However if the query failed or otherwise returned anything you were implicitly expecting you could have unforeseen errors. 但是,如果查询失败或以其他方式返回任何内容,则暗示您可能会遇到无法预料的错误。

You should encapsulate database output and database queries so that if they fail, they fail gracefully and do not have weird side effects that could be a pain to track down later. 您应该封装数据库输出和数据库查询,这样,如果它们失败,它们就会正常失败,并且不会产生怪异的副作用,以后可能很难找到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM