【问题标题】:Php postgresql search queryphp postgresql 搜索查询
【发布时间】:2014-12-09 14:17:36
【问题描述】:

我不确定我的查询是否正确,但我只是 PHP 和 postgresql 的初学者,因此我的代码。

我想要完成的是使用日期选择器进行搜索,它将提供来自日期的数据:

<?php
$output = '';
if(isset($_POST['search'])) {
    $searchq = $_POST['search'];

    $query = ("SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds.selling_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds
                                                    left join trees on tree_solds.tree_id = trees.id
                                                    left join transactions on transactions.id = tree_solds.transaction_id
                                                    WHERE date_purchased LIKE $searchq ");


                                        $result = pg_query($query); 
                                        if (!$result) { 
                                            echo "Problem with query " . $query . "<br/>"; 
                                            echo pg_last_error(); 
                                            exit(); 
                                        } 


                        $count = pg_num_rows($result);
                        if ($count == 0) {
                            $output = 'No Data on that date!';
                        } else {
                            while ($row = pg_fetch_array($result)) {
                               $output .= printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", htmlspecialchars($myrow['transaction_id']), htmlspecialchars($myrow['date_purchased']), htmlspecialchars($myrow['tree_type']), htmlspecialchars($myrow['actual_height']), htmlspecialchars($myrow['selling_height']), number_format($myrow['sub_total'], 2));
                            }
                        }
                    }
                ?>

HTML 表单

 <form action="location4.php" method="post">
 <input type="text" class="span2" name="search" value="" data-date-format="yyyy-mm-dd" id="dp2">

 <button type="submit" class="btn btn-default">Submit</button>
 </form>
 <?php print("$output");?>

不断收到此错误并且没有结果。

警告:pg_query():查询失败:错误:运算符不存在: 没有时区的时间戳 ~~ 整数 LINE 4: ... WHERE date_purchased LIKE 2014-... ^ 提示:没有运算符与给定的匹配 名称和参数类型。您可能需要添加显式类型转换。 在 /Applications/MAMP/htdocs/xmastool/location4.php 第 50 行问题 带查询 SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds. sell_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds left 加入tree_solds.tree_id = trees.id left 在 transactions.id = tree_solds.transaction_id WHERE 上加入交易 date_purchased LIKE 2014-12-07 错误:运营商不存在: 没有时区的时间戳 ~~ 整数 LINE 4: ... WHERE date_purchased LIKE 2014-... ^ 提示:没有运算符与给定的匹配 名称和参数类型。您可能需要添加显式类型转换。

【问题讨论】:

  • 您是否要考虑日期并忽略小时、分钟和秒来匹配时间戳?
  • 只匹配我所追求的日期,没有小时、分钟和秒。

标签: php postgresql search


【解决方案1】:

像这样使用:WHERE date_purchased LIKE '%$searchq%',如果你只想要它的一部分。

如果你想要完全匹配,那么WHERE date_purchased = '$searchq'

注意:%LIKE中的小丑角色,使用方法请查看手册。

【讨论】:

  • 添加了“%$searchq%”,但仍然出现与另一个错误几乎相同的错误,它显示结果 WHERE date_purchased LIKE '%2014-12-07%' 作为错误的一部分。
  • date_purchased 列的类型是什么?
  • date_purchased 是一个时间戳
【解决方案2】:

试试这个

pg_query_params('SELECT ... WHERE DATE_TRUNC('day', date_purchased) = $1', array($searchq))

请参阅[有关详细信息的文档。您应该始终确保正确转义查询参数(即不逐字连接到查询)以防止 SQL 注入。

【讨论】:

    【解决方案3】:

    由于polka_bolka的提示解决了这个问题。

     WHERE TO_CHAR(date_purchased, 'yyyy-mm-dd') LIKE '%$searchq%' ")
    

    将 date_purchased 转换为 char。

    <?php
    $output = '';
    if(isset($_POST['search'])) {
        $searchq = $_POST['search'];
    
        $query = ("SELECT trees.tree_type,tree_solds.transaction_id,tree_solds.actual_height,tree_solds.selling_height,tree_solds.sub_total,transactions.date_purchased FROM tree_solds
                                                        left join trees on tree_solds.tree_id = trees.id
                                                        left join transactions on transactions.id = tree_solds.transaction_id
                                                        WHERE TO_CHAR(date_purchased, 'yyyy-mm-dd') LIKE '%$searchq%' ");
    
    
                                            $result = pg_query($query); 
                                            if (!$result) { 
                                                echo "Problem with query " . $query . "<br/>"; 
                                                echo pg_last_error(); 
                                                exit(); 
                                            } 
    
    
                            $count = pg_num_rows($result);
                            if ($count == 0) {
                                $output = 'No Data on that date!';
                            } else {
                                while ($row = pg_fetch_array($result)) {
                                   $output .= printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", htmlspecialchars($row['transaction_id']), htmlspecialchars($row['date_purchased']), htmlspecialchars($row['tree_type']), htmlspecialchars($row['actual_height']), htmlspecialchars($row['selling_height']), number_format($row['sub_total'], 2));
                                }
                            }
                        }
                    ?>
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多