0

我正在尝试将数据库中的一些记录打印到表中:

    session_start();
if(!isset($_SESSION['studentnum'])){
    echo "Please "."<a href='login.php'>login</a>";
    exit;
}

$host = "localhost";
$username = "xyz";
$password = "abc";
$database = "mno";
$port = "5432";

$dbh = pg_connect("host=".$host." port=".$port." dbname=".$database." user=".$username." password=".$password);
if (!$dbh){
    die("Error in connection: ".pg_last_error());
}
$studentnum = $_SESSION['studentnum'];


$sql = "select * from project.student s, project.courses c 
where s.student_num = c.student_num and s.student_num='".$studentnum."'";
$result = pg_query($sql) or die('Query failed: ' . pg_last_error());
pg_free_result($result);
pg_close($dbh);

进而

<?php
while ($line = pg_fetch_array($result)) {  <= this is line 52
    echo "\t<tr>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
?>

但我收到此错误:

 SCREAM: Error suppression ignored for
( ! ) Warning: pg_fetch_array(): 3 is not a valid PostgreSQL result resource in D:\wamp\www\records.php on line 52

我做错什么了?我在其他地方使用了相同的代码并且它有效,所以我不确定这里缺少什么。

4

1 回答 1

0

我想到了。pg_free_result($result);使用前不小心打了电话$result

于 2013-04-02T03:06:18.350 回答