我不确定我是否正在进行查询,但我只是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():查询失败:ERROR:运算符不存在: 没有时区的时间戳~~整数LINE 4:... WHERE date_purchased LIKE 2014 -... ^提示:没有运算符匹配给定的 名称和参数类型。您可能需要添加显式类型转换。 在第50行的/Applications/MAMP/htdocs/xmastool/location4.php问题 查询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 tree trees on 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 -... ^提示:没有运算符匹配给定的 名称和参数类型。您可能需要添加显式类型转换。
答案 0 :(得分:0)
使用如下:WHERE date_purchased LIKE '%$searchq%'
如果你只想要它的一部分。
如果您想要完全匹配,请WHERE date_purchased = '$searchq'
注意: %
是LIKE
中的小丑角色,请查看手册的使用方法。
答案 1 :(得分:0)
试试这个
pg_query_params('SELECT ... WHERE DATE_TRUNC('day', date_purchased) = $1', array($searchq))
有关详细信息,请参阅[文档。您应始终确保正确转义查询参数(即,不会逐字连接到查询)以防止SQL注入。
答案 2 :(得分:0)
解决了这个问题。
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));
}
}
}
?>