title: Php PDO connection in postgres
body: <p>i have a php script which is called again and again with in a interval of 4 seconds by ajax.</p>
  
  <pre><code>try {
      $conn_p = new PDO("pgsql:host=$db_host;dbname=$db_dbname", $db_user, $db_password);
  } catch(PDOException $e) {  
      //$e-&gt;getMessage();
  }
  
  function abc($id)
  {
      global $conn_p;
  
      $st = "select * from table where id=:id";
      $sqlst=$conn_p-&gt;prepare($st);
      $bindParamArray=array("id"=&gt;$id);
      $sqlst-&gt;execute($bindParamArray);   
      $row=$sqlst-&gt;fetch();   
      return $row;
  }
  
  function xyz($no)
  {
      global $conn_p; 
      $st= "select * from table2 where no=:no and display='Y'";
      $sqlst=$conn_p-&gt;prepare($st);
      $bindParamArray=array(':no' =&gt; $no);
      $sqlst-&gt;execute($bindParamArray);   
      $row=$sqlst-&gt;fetch();   
      return $row;
  }
  
  function abc($id)
  {
      global $conn_p;
  
      $st = "select * from table where id=:id";
      $sqlst=$conn_p-&gt;prepare($st);
      $bindParamArray=array("id"=&gt;$id);
      $sqlst-&gt;execute($bindParamArray);   
      $row=$sqlst-&gt;fetch();   
      return $row;
  }
  
  function getData()
  {
      global $conn_p; 
      $st= "select * from table2";
      $sqlst=$conn_p-&gt;prepare($st);
      $sqlst-&gt;execute();  
      $row=$sqlst-&gt;fetchAll();    
      return $row;
  }
  ........same other functions
  
  $data = getData();
  
  foreach ($data as $dd) {
  
      $abc[] = abc($dd['id']);
      $xyz[] = xyz($dd['no']);
      //some other manipulations......
  }
  
  echo json_encode(array('data1'=&gt;$abc,'data2'=&gt;$xyz));
  </code></pre>
  
  <p>When running sql command </p>
  
  <pre><code>select * from pg_stat_activity
  </code></pre>
  
  <p>it is showing multiple connections.(told by my server admin to me)</p>
  
  <p>Now my question is that:</p>
  
  <ul>
  <li>Should i need to close the connetion ?</li>
  <li>If yes, then when to close it ?</li>
  <li>And what if some error occurs ?</li>
  </ul>