基本PHP PostgreSQL查询
这是我第一次使用PHP。我试图从表单中获取输入,根据输入进行Postgres查询,然后将结果打印到网页上 My index.html如下所示:基本PHP PostgreSQL查询,php,sql,postgresql,Php,Sql,Postgresql,这是我第一次使用PHP。我试图从表单中获取输入,根据输入进行Postgres查询,然后将结果打印到网页上 My index.html如下所示: <html> <body> <form action="table.php" method="get" enctype="multipart/form-data"> <select name="theMonth"> <o
<html>
<body>
<form action="table.php" method="get" enctype="multipart/form-data">
<select name="theMonth">
<option value="01">January</option>
<option value="02">February</option>
<!--(more month options excluded)-->
</select>
<select name="theDay">
<option value="01">01</option>
<option value="02">02</option>
<!--(More day options excluded)-->
</select>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
一月
二月
01
02
My table.php如下所示:
1 <?php
2 $conn= pg_connect("host=mySchool dbname= myDBNAME user=me password=myPassword");
3 if (!$conn) {
4 print "An error occurred.\n";
5 exit;
6 }
7
8 $result = pg_query($conn, "SELECT * FROM olympics WHERE EXTRACT(month from "closing_ceremony_date")=theMonth AND EXTRACT(day from "closing_ceremony_date") = theYear");
9 if (!$result) {
10 print "An error occurred.\n";
11 exit;
12 }
13
14 while ($row = pg_fetch_row($result)) {
15 print "Athlete: $row[0] Year: $row[3]";
16 print "<br />\n";
17 }
18
19 ?>
1
目前,提交表单会给我一个通用的内部服务器错误,我不确定如何从这里调试。我没有访问日志的权限,因为它托管在学校服务器上,我没有访问它们的权限
欢迎任何输入/指针 您忘记在sql上使用“$”符号或转义双引号:
因此,第8行将是:
$result = pg_query($conn, "SELECT * FROM olympics WHERE EXTRACT(month from $closing_ceremony_date)=theMonth AND EXTRACT(day from $closing_ceremony_date) = theYear");
或
你忘了用第15行的“{}”符号:
print "Athlete: {$row[0]} Year: {$row[3]}";
当您没有将
display\u error
s标志设置为on
(php.ini)并且php脚本中出现错误时,将显示内部服务器错误
错误来自您的sql
$result = pg_query($conn, "SELECT * FROM olympics WHERE EXTRACT(month from "closing_ceremony_date")=theMonth AND EXTRACT(day from "closing_ceremony_date") = theYear");
试着这样写:
$result = pg_query($conn, "SELECT * FROM olympics WHERE EXTRACT(month from closing_ceremony_date) = '$theMonth' AND EXTRACT(day from closing_ceremony_date) = '$theYear'");
我在这里假设闭幕式日期是数据表中的一列。Hm,谢谢。我已将其更改为此,但仍然收到一个内部服务器错误。您能否在php.ini文件中将
display\u error
切换到On
?这可能有助于调试。不幸的是,它是学校服务器上的只读文件,我的权限不允许我对其进行写入。不过,感谢您的想法。您的表单字段是name=“theMonth”
name=“theDay”
,您可以通过$\u POST['theMonth']
$\u POST['theDay']而不仅仅是theMonth
theYear
(另外,您的字段theDay
!=theYear
。此外,您的双引号可能导致语法错误,请尝试->“从奥运会中选择*摘录(从闭幕式开始的月份)={$\u POST['theMonth']}和摘录(从闭幕式开始的日期)={$\u POST['theYear']}
(假设您有一个名为name=“theYear”
的表单字段,而不仅仅是月/日。)
$result = pg_query($conn, "SELECT * FROM olympics WHERE EXTRACT(month from closing_ceremony_date) = '$theMonth' AND EXTRACT(day from closing_ceremony_date) = '$theYear'");