Jeff fff
Jeff fff

Reputation: 27

how to fix pg_query

this my code What's wrong in the code?

<?php
$host       =  "localhost";
$dbuser     =  "tesdb";
$dbpass     =  "123456";
$dbname    =  "tesdb";

$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass); 

$query ="select * from air_tanah.pembayaran";
$result = pg_query($dbcon, $query) or die('Query failed');

  // output result
  while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
   echo "    Denda: " . $line['denda'] ."    Penyimpan: " . $line['Penyimpan'] .  "<br/>";
  }

  // free result
  pg_free_result($result);

  // close connection
  pg_close($dbcon);
?>

when execution error like this

Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\grafig\read.php on line 12 Query failed

Upvotes: 0

Views: 140

Answers (1)

Try change your line:

$dbcon = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);

to

$dbcon = pg_connect("host=$host dbname=$dbname user=$dbuser password=$dbpass");

or if your PG server use not use default port (5432) you need to specify a port to connect like this, where xxxx is the prot number:

$dbcon = pg_connect("host=$host port=xxxx dbname=$dbname user=$dbuser password=$dbpass");

Hope this helps

Upvotes: 2

Related Questions