1

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 ?
3
  • Depending on the specifics of your setup you might want to consider using the persistent connection. php.net/manual/en/pdo.connections.php Commented Jan 3, 2018 at 15:09
  • You should consider closing the PDO with: $conn_p = null; Commented Jan 3, 2018 at 15:19
  • ok, when to use $conn_p = null; I mean in my above script where to write it, at end of script or ? Commented Jan 3, 2018 at 15:24

2 Answers 2

1

Some suggestions:

  1. No, you do not need to close the connection unless your script runs for a long time and you are not using it while it is doing its time-taking manipulations. The connection will be closed automatically when the script finishes.
  2. You should probably not fetch all rows from the database every x seconds. Only new or changed rows should be more efficient. And do you really need all rows in your page?
  3. You should not run ajax calls using a short interval. Instead do an ajax call and set a timeOut for the next call when the current ajax call finishes. That way requests will never overlap.
Sign up to request clarification or add additional context in comments.

Comments

0

THis is what I use in my class for postgresql.
It is same as for mysql.

$this->dsn = $this->driver . ':host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.