Good day, currently I have a problem when closing connections PostgreSQL
, with MySQL
no problem and the connection was closed easily just by using mysqli_close($conexion)
a little research to close connections with it would PostgreSQL
be as easy as pg_close($conexion)
however I get the following error:
Fatal error: Uncaught TypeError: pg_close(): Argument #1 ($connection) must be of type resource or null, PDO given in
And in the documentation of PHP
there is nothing more to add:
https://www.php.net/manual/en/function.pg-close.php
Syntax of my php script:
<?php
//Inicializo la conexion mediante funciones
include('conexion.php');
$objeto = new Conexion();
$conexion = $objeto->Conectar();
$id_categoria=$_GET['id_categoria'];
//Ejecuto la consulta
$sql = "select id_producto, nombre, precio_vta from inv_productos where
id_categoria = '$id_categoria'";
$query = $conexion->query($sql);
$datos = array();
if($id_categoria > 0){
while($resultado = $query->fetch(PDO::FETCH_ASSOC)) {
$datos[] = $resultado;
}
}
//Imprimo el resultado
echo json_encode(array("Productos" => $datos));
//Cierro la conexión
pg_close($conexion);
?>
What is the problem? or is there any alternative to close connections? I hope you can help me if you need the complete code of the script PHP
please tell me, thanks in advance.
As I pointed out in the comments, the error message is quite informative. It is indicating that the type that the function expects as an argument
pg_close
is resource and you are passing it aPDO
(PHP Data Object).If we Google in English how to close a PDO connection, we will find among the first results this question from our sister forum, whose answer refers us to the PHP documentation .
That in Spanish they have translated as
Right after this paragraph in the aforementioned documentation we can find an example of connection closure that matches what you are looking for
You're mixing pears with apples.
We are talking about two different extensions to connect to the database:
To close the connection with
pg_close
you should have opened it withpg_connect
.If you use PDO, it is enough to assign
null
to the object and the previously used resources; although that is done automatically at the end of the script execution; In addition, the manual indicates:In other words, there is little point in closing the connection.