drupal数据库查询问题

发布于 2024-09-19 06:31:48 字数 794 浏览 0 评论 0原文

我尝试使用此请求获取结果,该请求在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2024-09-26 06:31:48

该问题可能是由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文