警告:pg_fetch_array():3不是有效的PostgreSQL结果资源

时间:2013-04-02 02:07:26

标签: php postgresql

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

    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

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

1 个答案:

答案 0 :(得分:0)

我明白了。在使用pg_free_result($result);

之前,我偶然致电$result