- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 PHP PDO 通过 JSON 将对象检索到 iOS 应用程序。有一个包含事件对象的数据库表。它有三个字段来存储日、月和年。数据库的所有者不想在日期字段中存储日期,这意味着我必须处理它。
应用发送带有参数的 URL,如下所示:
http://..hidden domain./myfile.php?dia_inicio=16&dia_final=15&mes_inicio=11&mes_final=12&ano_inicio=2014&ano_final=2015
这意味着我正在寻找从 16Nov2014 到 15Dec2015 的事件。
这是我的 PHP 代码:
$sql = 'SELECT * FROM tbagenda WHERE (dia_evento => :di AND mes_evento = :mi AND ano_evento = :ai) OR (dia_evento <= :df AND mes_evento = :mf AND ano_evento = :af) ';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
$stmt->bindParam(':di', $dia_inicio, PDO::PARAM_INT);
$stmt->bindParam(':df', $dia_final, PDO::PARAM_INT);
$stmt->bindParam(':mi', $mes_inicio, PDO::PARAM_INT);
$stmt->bindParam(':mf', $mes_final, PDO::PARAM_INT);
$stmt->bindParam(':ai', $ano_inicio, PDO::PARAM_INT);
$stmt->bindParam(':af', $ano_final, PDO::PARAM_INT);
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json
我应该如何更改声明以使其适用于以下日期:2014 年 11 月 16 日至 2014 年 11 月 28 日(同月)、2014 年 16 月 16 日至 2014 年 12 月 5 日(同年不同月份)和 2014 年 11 月 16 日至 2015 年 5 月 2 日(不同年份)?
稍后添加:
iOS 日志显示发送到 PHP 文件的 URL:
recuperar_eventos_dia.php?dia_inicio=11&dia_final=26&mes_inicio=11&mes_final=11&ano_inicio=2014&ano_final=2014
PHP 部分:
$start = "$ano_inicio-$mes_inicio-$dia_inicio";
$end = "$ano_final-$mes_final-$dia_final";
// In the SQL, concatenate the columns to make a YYYY-MM-DD date string
// and cast it to a MySQL DATE type.
// That makes it possible to use BETWEEN
$sql = 'SELECT
*
FROM tbagenda
WHERE STR_TO_DATE(CONCAT_WS('-', ano_evento, mes_evento, dia_evento), "%Y-%m-%d")
BETWEEN :start AND :end';
// Bind and execute the statement with 2 parameters:
$stmt = $dbh->prepare( $sql );
$stmt->bindParam(':start', $start, PDO::PARAM_STR);
$stmt->bindParam(':end', $end, PDO::PARAM_STR);
$stmt->execute();
// fetch, etc...
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// convert to json
$json = json_encode( $result );
// echo the json string
echo $json;
现在是表 tbagenda 的屏幕截图:
最佳答案
我很抱歉你被这个不幸的表结构困住了。
我建议连接表的列以形成可用的日期字符串,并通过 MySQL 的内置函数 STR_TO_DATE()
将它们转换为日期值。 .对查询字符串输入值执行相同的操作,以使用 BETWEEN
运算符进行正确的日期比较。
// Begin by concatenating the input values into single date strings YYYY-MM-DD
$start = "$ano_inicio-$mes_inicio-$dia_inicio";
$end = "$ano_final-$mes_final-$dia_final";
// In the SQL, concatenate the columns to make a YYYY-MM-DD date string
// and cast it to a MySQL DATE type.
// That makes it possible to use BETWEEN
$sql = "SELECT *
FROM tbagenda
WHERE STR_TO_DATE(CONCAT_WS('-', ano_evento, mes_evento, dia_evento), '%Y-%m-%d')
BETWEEN :start AND :end";
// Bind and execute the statement with 2 parameters:
$stmt = $dbh->prepare( $sql );
$stmt->bindParam(':start', $start, PDO::PARAM_STR);
$stmt->bindParam(':end', $end, PDO::PARAM_STR);
$stmt->execute();
// fetch, etc...
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
字符串操作会影响这个查询的性能。如您所知,如果日期存储为适当的 DATE
会更好,这样 MySQL 就不需要转换它们来进行比较。然后还可以将它们编入索引。
另请注意,我没有包括日期字符串的验证。您可以考虑在执行查询之前检查它们以确保它们是有效日期。
// strtotime() would return false for invalid date strings...
if (strtotime($start) && strtotime($end)) {
// The dates are valid, and so you can proceed with the query
$sql = 'SELECT.....';
}
关于PHP PDO语句过滤记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27073542/
创建一个“海盗对话”,可以选择左手或右手。我希望它对“左”和“右”的不同拼写做出积极的回答(正如您将在代码中看到的那样),但是,当我为所有非“右”或“左”的输入添加最终的“else”代码时,它给了我一
With 语句 对一个对象执行一系列的语句。 With object statements End With 参数 object 必需的部分
While...Wend 语句 当指定的条件为 True 时,执行一系列的语句。 While condition  ; Version [stat
所以我正在处理的代码有一个小问题。 while True: r = input("Line: ") n = r.split() if r == " ":
我有一个对象数组: var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number"
int main() { int f=fun(); ... } int fun() { return 1; return 2; } 在上面的程序中,当从main函数中调用一个
我的项目中有很多 if 语句、嵌套 if 语句和 if-else 语句,我正在考虑将它们更改为 switch 语句。其中一些将具有嵌套的 switch 语句。我知道就编译而言,switch 语句通常更
Rem 语句 包含程序中的解释性注释。 Rem comment 或 ' comment comment 参数是需要包含的注释文本。在 Rem 关键字和 comment 之间应有一个空格。
ReDim 语句 在过程级中声明动态数组变量并分配或重新分配存储空间。 ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Randomize 语句 初始化随机数生成器。 Randomize [number] number 参数可以是任何有效的数值表达式。 说明 Randomize 使用 number 参数初始
Public 语句 定义公有变量并分配存储空间。在 Class 块中定义私有变量。 Public varname[([subscripts])][, varname[([subscripts])
Sub 语句 声明 Sub 过程的名称、参数以及构成其主体的代码。 [Public [Default]| Private] Sub name [( arglist )]
Set 语句 将对象引用赋给一个variable或property,或者将对象引用与事件关联。 Set objectvar = {objectexpression | New classname
我有这个代码块,有时第一个 if 语句先运行,有时第二个 if 语句先运行。我不确定为什么会这样,因为我认为 javascript 是同步的。 for (let i = 0; i < dataObje
这是一个 javascript 代码,我想把它写成这样:如果此人回答是,则回复“那很酷”,如果此人回答否,则回复“我会让你开心”,如果此人回答的问题包含"is"或“否”,请说“仅键入”是或否,没有任何
这是我的任务,我尝试仅使用简短的 if 语句来完成此任务,我得到的唯一错误是使用“(0.5<=ratio<2 )”,除此之外,构造正确吗? Scanner scn = new Scanner(
有没有办法在 select 语句中使用 if 语句? 我不能在这个中使用 Case 语句。实际上我正在使用 iReport 并且我有一个参数。我想要做的是,如果用户没有输入某个参数,它将选择所有实例。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: If vs. Switch Speed 我将以 C++ 为例,但我要问的问题不是针对特定语言的。我的意思是一
Property Set 语句 在 Class 块中,声明名称、参数和代码,这些构成了将引用设置到对象的 Property 过程的主体。 [Public | Private] Pro
Property Let 语句 在 Class 块中,声明名称、参数和代码等,它们构成了赋值(设置)的 Property 过程的主体。 [Public | Private] Prop
我是一名优秀的程序员,十分优秀!