如何为给定任务使用while条件?

时间:2012-06-05 17:44:08

标签: php mysql sql

我使用日期

获取用户详细信息
if(date("d")==1) {
    $time  = "m";
}
if(date("l")=="Friday") {
    $time  = "w";
}
$time  = "d";

$sql=mysql_query("select user from table_users where $reprt=".$time .");
while($row=mysql_fetch_Array($sql) {
    $sql==mysql_query("select * from table_register where $user="$row[0]");
}

这里当(date("d")==1)我想用d和m选择语句时,这是怎么回事。

3 个答案:

答案 0 :(得分:0)

假设你有一个字符串。将它命名为S.你也有一个字符串 query =“select * from table_register where”

你需要做的是

 query = "select * from table_register where "  
  if(date("d")==1){
           s = "$report_frequency  = \"m\" OR $report_frequency  = \"d\" ";
    }
    elseif(date("l")=="Friday"){
         s = "$report_frequency  = \"m\" ";
    }
    else{
    s = "$report_frequency  = \"d\" ";
    }

    query = query.s;

    $sql=mysql_query(query);
    ...

换句话说,使用字符串

动态构造查询字符串

答案 1 :(得分:0)

在您的代码中,您在IF之外使用$time = "d";,因此您始终拥有$time=d,因此请将代码替换为:

if(date("d")==1){
  $time  = "m or $reprt='d'";
}
if(date("l")=="Friday"){
  $time  = "w";
}

$sql=mysql_query("select user from table_users where $reprt=".$time);
while($row=mysql_fetch_Array($sql){
  $sql=mysql_query("select * from table_register where $user='$row[0]'");
}

答案 2 :(得分:0)

让我们看看我是否做对了......如果我理解正确,你应该只在一个查询中这样做

从table_register中选择*其中$ user in(从table_users中选择用户,其中$ reprt =“。$ time)

将是一个更好的查询然后你已经设置...仍然不是最好的,但至少它只有1个查询。

对于实际问题,您应该将代码更改为以下

if(date("d")==1)
{
 $time  = "m";
}
if(date("l")=="Friday")
{
 $time  = "w";
}

if($time == "m") // this is when you want to also search for d right?
{
  //we do what I suggested earlier with a slight change
  mysql_query("select * from table_register where $user in (select user from table_users where  $reprt=".$time." OR $reprt=d)");
}
else
{
  //we do what I suggested earlier
  mysql_query("select * from table_register where $user in (select user from table_users where  $reprt=".$time.")");
}

希望这有帮助。