pg_fetch_array problem

pg_fetch_array problem

am 08.08.2002 18:50:30 von angelo.rigo

Hi i am trying to build
previous and next links from a table selection, the code below stops working
at the line which says :

while ($dados =3D pg_fetch_array($limite)) {
$nome =3D $dados["nome"];
echo "Nome: $nome
";

the warning says:
Wrong parameter count for pg_fetch_array()

i get this code from a php mysql and try to adapt to my needs, is that any
parameter wrong? wich can be?



$conn =3D pg_connect("dbname=3Dsondagem user=3Dpostgres");
?>
$busca =3D "SELECT nome AS data FROM aprovados ORDER BY nome ASC ";
?>
$total_reg =3D "10"; // n=FAmero de registros por p=E1gina
?>
if (!$pagina) {
$pc =3D "1";
} else {
$pc =3D $pagina;
}
?>
$inicio =3D $pc - 1;
$inicio =3D $inicio * $total_reg;
?>
$limite =3D pg_exec("$busca LIMIT $inicio,$total_reg");
$todos =3D pg_exec("$busca");

$tr =3D pg_numrows($todos); // verifica o n=FAmero total de registros
$tp =3D $tr / $total_reg; // verifica o n=FAmero total de p=E1ginas

// vamos criar a visualização
while ($dados =3D pg_fetch_array($limite)) {
$nome =3D $dados["nome"];
echo "Nome: $nome
";
}

// agora vamos criar os bot=F5es "Anterior e pr=F3ximo"
$anterior =3D $pc -1;
$proximo =3D $pc +1;
if ($pc>1) {
echo " ";
}
echo "|";
if ($pc<$tp) {
echo " ";
}
?>

________________________________________
A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com=
..br.



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: pg_fetch_array problem

am 08.08.2002 18:57:14 von dbrown

hmm.. you could try..

for(var i=0; $dados = pg_fetch_array($limite, $i); i++) {
$nome = $dados["nome"];
echo "Name: $nome
";
}

I believe your error is your not passing pg_fetch_array() the row you
wish to return. It *may* or may not work that way. But leaving it open
like that is sure to cause an error sooner or later.

Hope this helps
David

angelo.rigo@globo.com wrote:

>Hi i am trying to build
>previous and next links from a table selection, the code below stops working
>at the line which says :
>
>while ($dados = pg_fetch_array($limite)) {
> $nome = $dados["nome"];
> echo "Nome: $nome
";
>
>the warning says:
>Wrong parameter count for pg_fetch_array()
>
>i get this code from a php mysql and try to adapt to my needs, is that any
>parameter wrong? wich can be?
>
>
> >
>$conn = pg_connect("dbname=sondagem user=postgres");
>?>
> >$busca = "SELECT nome AS data FROM aprovados ORDER BY nome ASC ";
>?>
> >$total_reg = "10"; // número de registros por página
>?>
> >if (!$pagina) {
> $pc = "1";
>} else {
> $pc = $pagina;
>}
>?>
> >$inicio = $pc - 1;
>$inicio = $inicio * $total_reg;
>?>
> >$limite = pg_exec("$busca LIMIT $inicio,$total_reg");
>$todos = pg_exec("$busca");
>
>$tr = pg_numrows($todos); // verifica o número total de registros
>$tp = $tr / $total_reg; // verifica o número total de páginas
>
>// vamos criar a visualização
>while ($dados = pg_fetch_array($limite)) {
> $nome = $dados["nome"];
> echo "Nome: $nome
";
>}
>
>// agora vamos criar os botões "Anterior e próximo"
>$anterior = $pc -1;
>$proximo = $pc +1;
>if ($pc>1) {
> echo " ";
>}
>echo "|";
>if ($pc<$tp) {
> echo " ";
>}
>?>
>
>________________________________________
>A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br.
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Don't 'kill -9' the postmaster
>
>
>



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: pg_fetch_array problem

am 08.08.2002 19:03:38 von Scott Marlowe

What version of PHP are you running? The autoincrementing nature of the
row value in pg_fetch_array wasn't included until PHP 4.1.0

On Thu, 8 Aug 2002 angelo.rigo@globo.com wrote:

> Hi i am trying to build
> previous and next links from a table selection, the code below stops working
> at the line which says :
>
> while ($dados = pg_fetch_array($limite)) {
> $nome = $dados["nome"];
> echo "Nome: $nome
";
>
> the warning says:
> Wrong parameter count for pg_fetch_array()
>
> i get this code from a php mysql and try to adapt to my needs, is that any
> parameter wrong? wich can be?
>
>
> >
> $conn = pg_connect("dbname=sondagem user=postgres");
> ?>
> > $busca = "SELECT nome AS data FROM aprovados ORDER BY nome ASC ";
> ?>
> > $total_reg = "10"; // número de registros por página
> ?>
> > if (!$pagina) {
> $pc = "1";
> } else {
> $pc = $pagina;
> }
> ?>
> > $inicio = $pc - 1;
> $inicio = $inicio * $total_reg;
> ?>
> > $limite = pg_exec("$busca LIMIT $inicio,$total_reg");
> $todos = pg_exec("$busca");
>
> $tr = pg_numrows($todos); // verifica o número total de registros
> $tp = $tr / $total_reg; // verifica o número total de páginas
>
> // vamos criar a visualização
> while ($dados = pg_fetch_array($limite)) {
> $nome = $dados["nome"];
> echo "Nome: $nome
";
> }
>
> // agora vamos criar os botões "Anterior e próximo"
> $anterior = $pc -1;
> $proximo = $pc +1;
> if ($pc>1) {
> echo " ";
> }
> echo "|";
> if ($pc<$tp) {
> echo " ";
> }
> ?>
>
> ________________________________________
> A busca mais veloz e precisa da internet. Acesse agora: http://www.zoom.com.br.
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

Re: pg_fetch_array problem

am 08.08.2002 19:04:55 von Scott Marlowe

On Thu, 8 Aug 2002, David C. Brown wrote:

> hmm.. you could try..
>
> for(var i=0; $dados = pg_fetch_array($limite, $i); i++) {
> $nome = $dados["nome"];
> echo "Name: $nome
";
> }
>
> I believe your error is your not passing pg_fetch_array() the row you
> wish to return. It *may* or may not work that way. But leaving it open
> like that is sure to cause an error sooner or later.

Actually, no. In the latest and greatest version of PHP, pg_fetch_array
returns false when it runs out of rows, so you can just use:

while ($row = pg_fetch_array){
dosomething here...
}


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Re: pg_fetch_array

am 15.06.2004 02:04:57 von ljb

Thanks@verymuch.com wrote:
> Hello....
>
> I would like to know the performance of pg_fetch_array. Cosider the code:
>
> $query = "select * from foo";
> $result = pg_query( $db, $query );
>
> while ($row = pg_fetch_array($result))
> {
> $a = $row["a"];
> $b = $row["b"];
> $c = $row["c"];
> $d = $row["d"];
> }
>
> Does php need to read database everytime when pg_fetch_array is executed in
> the while loop or all the rows have been in the memory after pg_query?

The whole result query is stored in memory when pg_query returns, and
pg_fetch_array just gets the next row.

> If read database is needed, is there any method to copy all the things into
> memory by using other command? (because I have a application which needs
> large amount database update/retrieval and I wish the performance of the
> overall applications run faster.)
>
> or other method you would like to recommend in order to make the faster
> response time?

Actually, bringing the whole result into memory before processing any rows
can be slower, since the web server has to wait for the database server to
finish before it can start working on the data. So you can't get any
overlap of database and web server processing. As far as I know, there
isn't any alternative currently in the PHP PostgreSQL interface; there are
asynchronous queries but you still can't process any rows until they all
come back.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Re: pg_fetch_array

am 15.06.2004 06:51:26 von Scott Marlowe

On Mon, 2004-06-14 at 18:04, ljb wrote:
> Thanks@verymuch.com wrote:
> > Hello....
> >
> > I would like to know the performance of pg_fetch_array. Cosider the code:
> >
> > $query = "select * from foo";
> > $result = pg_query( $db, $query );
> >
> > while ($row = pg_fetch_array($result))
> > {
> > $a = $row["a"];
> > $b = $row["b"];
> > $c = $row["c"];
> > $d = $row["d"];
> > }
> >
> > Does php need to read database everytime when pg_fetch_array is executed in
> > the while loop or all the rows have been in the memory after pg_query?
>
> The whole result query is stored in memory when pg_query returns, and
> pg_fetch_array just gets the next row.
>
> > If read database is needed, is there any method to copy all the things into
> > memory by using other command? (because I have a application which needs
> > large amount database update/retrieval and I wish the performance of the
> > overall applications run faster.)
> >
> > or other method you would like to recommend in order to make the faster
> > response time?
>
> Actually, bringing the whole result into memory before processing any rows
> can be slower, since the web server has to wait for the database server to
> finish before it can start working on the data. So you can't get any
> overlap of database and web server processing. As far as I know, there
> isn't any alternative currently in the PHP PostgreSQL interface; there are
> asynchronous queries but you still can't process any rows until they all
> come back.

Yes, operating on the whole set in memory at one time is not the best
job for PHP, but maybe for PostgreSQL.

And there is a way to retrieve some small percentage at a time, use a
cursor. See declare and fetch in the manual. Works a charm in PHP.


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org