Jump to content

php cant print all the rows from database


sakibd2k

Recommended Posts

Hello,

 

I am trying to query and show it by fetching all the row  but actually only one row shows up

not all the rows by defined condition.

 

The code is

 

<?php

$db=pg_connect("host=localhost dbname=xxxx  user=xxxx  password=xxxx ");

..........................................................

 

 

$result=pg_query($db,"select  id,credit from cc_cdr where id<=20500 AND id>=10501 AND credit<5 ORDER BY id ASC");

 

$row=pg_fetch_array($result);

echo $row["id"];

echo  $row["credit"];

 

?>

 

i can see only one , the first row

if i change pg_fetch_all($result)

 

nothing show up

 

what will  i do in this case?

 

 

Here is the exact description from php.net: "pg_fetch_array — Fetch a row as an array"

 

So, you'll have to loop through all the rows. Something like this:

$all_rows = array();
while ($row = pg_fetch_array($result)){
  $all_rows[] = $row;
}
print_r($all_rows);

pg_fetch_array() only fetches one row from the result set each time it is called. You would need to use it in a loop (i.e. what F1Fan posted above) to iterate over all the rows in the result set.

 

pg_fetch_all() fetches all the rows from the result set into an array. You would need to use php's array functions, such as foreach(), to iterate over all the rows in the array.

Thank you F1fan. I knew i was missing the array or something like that but do not know how does it solve.

 

I am very happy at least this one solved.

 

But the out put showing all the thing like following

 

Array ( [0] => Array ( [0] => 10505 [id] => 10505 [1] => 0.1016 [credit] => 0.1016 [2] => 2259217297 [username] => 2259217297 ) [1] => Array ( [0] => 10506 [id] => 10506 [1] => 0.0260 [credit] => 0.0260 [2] => 9273294177 [username] => 9273294177 ) [2] => Array ( [0] => 10507 [id] => 10507 [1] => 0.3040 [credit] => 0.3040 [2] => 6944787983 [username] => 6944787983 ) [3] .................................................

 

I want to show them like following

 

id        credit          username

 

10505  0.1016    2259217297

........................................

 

to view This manner  what i need to do?

 

Thanks F1Fan

one last request  is it than possible

 

when i put condition under select query put it in variable  which will be choosed by user

 

i mean when i am using this query in parantheses  i want here instead of id<=20500  id<=$var

 

select  id,credit from cc_cdr where id<=20500 AND id>=10501 AND credit<5 ORDER BY id ASC

 

Where $var will be given by input in html file and php will process

 

this way at least thing will come easier.

 

Thanks again

Of course it's possible. I don't have the time to write the code for you, but you can follow the tutorials on this site: http://www.w3schools.com/php/php_mysql_select.asp

 

You can get what you're trying by combining several of the tutorials.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.