-3

我正在使用日期获取用户详细信息

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 选择语句时,这怎么可能。

4

3 回答 3

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);
    ...

换句话说,使用 Strings 动态构建查询字符串

于 2012-06-05T17:53:02.510 回答
0

在您的代码中,您在$time = "d";IF 之外使用,因此您将始终拥有$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]'");
}
于 2012-06-05T17:53:11.107 回答
0

让我们看看我是否正确...如果我理解正确,您应该只在一个查询中执行此操作

select * from table_register where $user in (select user from table_users where $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.")");
}

希望这可以帮助。

于 2012-06-05T17:56:30.607 回答