i have a php script which is called again and again with in a interval of 4 seconds by ajax.
try {
$conn_p = new PDO("pgsql:host=$db_host;dbname=$db_dbname", $db_user, $db_password);
} catch(PDOException $e) {
//$e->getMessage();
}
function abc($id)
{
global $conn_p;
$st = "select * from table where id=:id";
$sqlst=$conn_p->prepare($st);
$bindParamArray=array("id"=>$id);
$sqlst->execute($bindParamArray);
$row=$sqlst->fetch();
return $row;
}
function xyz($no)
{
global $conn_p;
$st= "select * from table2 where no=:no and display='Y'";
$sqlst=$conn_p->prepare($st);
$bindParamArray=array(':no' => $no);
$sqlst->execute($bindParamArray);
$row=$sqlst->fetch();
return $row;
}
function abc($id)
{
global $conn_p;
$st = "select * from table where id=:id";
$sqlst=$conn_p->prepare($st);
$bindParamArray=array("id"=>$id);
$sqlst->execute($bindParamArray);
$row=$sqlst->fetch();
return $row;
}
function getData()
{
global $conn_p;
$st= "select * from table2";
$sqlst=$conn_p->prepare($st);
$sqlst->execute();
$row=$sqlst->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'=>$abc,'data2'=>$xyz));
When running sql command
select * from pg_stat_activity
it is showing multiple connections.(told by my server admin to me)
Now my question is that:
- Should i need to close the connetion ?
- If yes, then when to close it ?
- And what if some error occurs ?