drupal数据库查询问题
我尝试使用此请求获取结果,该请求在 phpayadmin 中工作:
$result_med = db_query("SELECT node.nid AS nid,
node.created AS node_created
FROM dr_wiwe_node node
LEFT JOIN dr_wiwe_content_type_classified node_data_field_classified_valid_till ON node.vid = node_data_field_classified_valid_till.vid
WHERE ((node.type in ('classified')) AND (node.status <> 0))
AND (DATE_FORMAT(STR_TO_DATE(node_data_field_classified_valid_till.field_classified_valid_till_value, '%Y-%m-%dT%T'), '%Y-%m-%d\T%H:%i:%s') >= '2010-09-16T22:34:05')
ORDER BY node_created DESC LIMIT 1");
var_dump($result_med);
while ($node = db_fetch_object($result_med)) {
//var_dump ($node);}
在硬编码的 php 版本中,它不返回任何内容。如果我 var_dump $result_med,我得到: 类型(mysql 结果)的资源(552)
我的错误在哪里?
i try to fetch a result with this request, which works in phpayadmin:
$result_med = db_query("SELECT node.nid AS nid,
node.created AS node_created
FROM dr_wiwe_node node
LEFT JOIN dr_wiwe_content_type_classified node_data_field_classified_valid_till ON node.vid = node_data_field_classified_valid_till.vid
WHERE ((node.type in ('classified')) AND (node.status <> 0))
AND (DATE_FORMAT(STR_TO_DATE(node_data_field_classified_valid_till.field_classified_valid_till_value, '%Y-%m-%dT%T'), '%Y-%m-%d\T%H:%i:%s') >= '2010-09-16T22:34:05')
ORDER BY node_created DESC LIMIT 1");
var_dump($result_med);
while ($node = db_fetch_object($result_med)) {
//var_dump ($node);}
In the hardcoded php Version it returns nothing. If I var_dump $result_med, I am getting:
resource(552) of type (mysql result)
Where is my error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题可能是由于
db_query()
将部分日期时间格式字符串视为查询参数并试图替换这些参数而引起的。因此,您需要向现有字符添加额外的“%”字符以转义它们,从而防止参数替换过程尝试替换它们。
请参阅“如果查询中包含 %”评论 来自 db_query api 文档的示例。
一个更清晰/更具可读性的解决方案可能是仅使用“%s”占位符作为查询中的格式化字符串,然后将实际的格式化字符串作为参数添加到 db_query 调用中,如 Eli 建议的那样。
The problem is probably caused by
db_query()
treating parts of your datetime formatting strings as query parameters, which it tries to replace.So you'll need to add additional '%' characters to your existing ones to escape them, thus preventing the parameter substitution process from trying to replace them.
See the "If a query that has % in them" comment from the db_query api documentation for an example.
A cleaner/more readable solution might be to just use '%s' placeholders for the formatting strings in the query and then add the actual formatting strings as arguments to the db_query call, as suggested by Eli.