好的,所以我提出了这个编码。这是我在PHP中用于在HTML页面上显示的PGSQL查询。我正在使用正确的PHP来显示手动输入的日期。我想要做的是替换PGSQL的最后一行:
SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= '2011-09-01' AND cm."Date" < '2011-10-01';
我希望将变量从HTML表单传递给PHP,这样一旦用户点击提交,它就会以所选日期为准。真的,上面显示了一个完整的日期。我只想从下拉选择框中选择一个月,然后输入的日期将是2011-09-01至2011-10-01九月,或2011-08-01至2011-09- 01年8月,依此类推。我真的需要一些帮助。
我的php现在看起来像这样显示查询:
$result = pg_query($query) or die('Query failed: ' .
pg_last_error());
// Printing results in HTML
echo "<h2 align=center>Revenue Report</h2>";
echo "<table align=center border=1 solid width=500px>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";// Free resultset
pg_free_result($result);// Closing connection
pg_close($dbconn);
答案 0 :(得分:0)
您可以使用 plpgsql函数(存储过程)。
CREATE OR REPLACE FUNCTION f_test(integer)
RETURNS TABLE(
"ID" integer -- !guessing your data types, you may have to adjust!
,"DSI" integer
,"Date" date
,"Amount" integer
,"Name" text
,"Comments" text) AS
$BODY$
DECLARE
mydate date := (to_char(now(), 'YYYY') || '-' || $1::text || '-1')::date;
BEGIN
RETURN QUERY
SELECT cm."ID"
,a."ID" -- AS "DSI"
,cm."Date"
,c."Amount"
,ct."Name"
,cm."Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= mydate AND cm."Date" < (mydate + interval '1 month')::date;
END;
$BODY$
LANGUAGE plpgsql;
致电(11月):
SELECT * FROM f_test(11);
答案 1 :(得分:0)
HTML:
<select name="startdate">
<option value="2014-01-01">2014-01-01</option>
</select>
PHP:
$startdate = pg_escape_string($_REQUEST['startdate']);
$query = <<<SQL
SELECT cm."ID",a."ID" as "DSI",cm."Date",c."Amount",ct."Name" as "Name",cm."Comments" as "Comments"
FROM "Memo" cm
LEFT JOIN "Credit" c ON (c."ID" = cm."CreditID")
LEFT JOIN "Account" a ON (c."AccountID" = a."ID")
LEFT JOIN "CreditMemoReason" ct ON (ct."ID" = cm."CreditMemoReasonID")
WHERE cm."Date" >= {$startdate} AND cm."Date" < ({$startdate}+ interval '1 month')::date;
SQL;