-2

I am new in php, and wonder why I getting this, need expert to guide. Thanks.

PHP Fatal error: Call to a member function rowCount() on resource in C:\inetpub\wwwroot\xxx\xxx.php on line 9

<?php
include 'connect_db.php';
$conn = null;

$sqlGetFeedback = "Select * from t_abc";

$resFB = $conn->query($sqlGetFeedback);
$rows = array();

if($resFB->rowCount()){
    echo json_encode($resFB->fetchAll(PDO::FETCH_ASSOC));
} else {    
    echo '[{}]';
}
?>
nat8991
  • 31
  • 2
  • 8

2 Answers2

0

$conn is an object reference that you have set to null which essentially means "no object there". And then you call the method query that operates on an object, which you don't have because it is null.

In your included file connect_db.php maybe there is a constructor for the object that implements the method query or there is a factory function that returns a constructed object that implements query.

I hope you know some basics about object oriented programming, or else all this perhaps won't help you much. In this case grab a book learn about this programming paradigm first.

Martin Sugioarto
  • 340
  • 3
  • 15
  • I found the problem is not from the $conn, but is from the function called rowCount is the sample code from mysql. I need it in Postgresql. Any advice how to replace the rowCount in Postgresql statement? – nat8991 Sep 05 '16 at 14:32
0

Your connection is referencing the $conn = null. Soln: It's either you place the include connect_db.php after the $conn

Or

Remove the $conn = null entirely. You should have $conn in your connect_db.php

Iakhator
  • 159
  • 2
  • 12
  • I found the problem is not from the $conn, but is from the function called rowCount is the sample code from mysql. I need it in Postgresql. Any advice how to replace the rowCount in Postgresql statement? – nat8991 Sep 05 '16 at 14:46
  • Let's see your connect_db.php script – Iakhator Sep 07 '16 at 07:09