-3

我的 PHP 脚本可以正常工作,如下所示:

while($row4 = mysql_fetch_array($result4))
{
if (($row4['Username'] != 'root') && ($row4['ToUpline'] == 1)){
if (!(($row4['Username'] != $MyUsername) && ($row4['Private'] == 1))){

//some other scripts here...

}};

另一方面,我已经有了 MySQL 命令的 WHERE 条件:

WHERE (NetworkTree.LeftNum <= $MyLeftNum AND NetworkTree.RightNum >= $MyRightNum) 
AND MyBlackList.HideWho IS NULL

如何将这些 PHP 条件组合成 1 个 SQL 命令,以便我可以删除 PHP IF。谢谢...

4

2 回答 2

3
WHERE
  NetworkTree.LeftNum <= $MyLeftNum AND
  NetworkTree.RightNum >= $MyRightNum AND
  MyBlackList.HideWho IS NULL AND
  Username <> 'root' AND
  ToUpline = 1 AND
  NOT(Username <> 'myUsername' AND
      Private = 1)

您如何填写'myUsername'上面的内容取决于您执行查询的方式。由于您使用的是过时的 mysql_*函数,因此您不能使用准备好的语句,但您可以使用字符串连接或sprintf(不要忘记使用mysql_real_escape_string进行转义!)。以下是使用 sprintf 的方法:

$query = sprintf("SELECT ... WHERE ... NOT(Username <> '%s' ...)",
                 mysql_real_escape_string($username));
于 2012-06-20T05:53:34.653 回答
0
WHERE (NetworkTree.LeftNum <= $MyLeftNum AND NetworkTree.RightNum >= $MyRightNum) AND MyBlackList.HideWho IS NULL AND Username != 'root' AND ToUpLine = 1 AND NOT (Username != '$MyUsername' AND Private = 1)
于 2012-06-20T05:53:23.147 回答