sakibd2k Posted August 27, 2009 Share Posted August 27, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/ Share on other sites More sharing options...
F1Fan Posted August 27, 2009 Share Posted August 27, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907923 Share on other sites More sharing options...
PFMaBiSmAd Posted August 27, 2009 Share Posted August 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907925 Share on other sites More sharing options...
sakibd2k Posted August 27, 2009 Author Share Posted August 27, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907930 Share on other sites More sharing options...
F1Fan Posted August 27, 2009 Share Posted August 27, 2009 Are you just wanting to display all the data for yourself, or formatted for users to view? Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907937 Share on other sites More sharing options...
sakibd2k Posted August 27, 2009 Author Share Posted August 27, 2009 Just for myself in orderly manner I could view this from database server siiting in ront of it but i am doing it so that i can view it remotley in browser. Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907943 Share on other sites More sharing options...
F1Fan Posted August 27, 2009 Share Posted August 27, 2009 Well, if you add the <pre> </pre> tags around the print_array(), that will help, or you could write some code to echo it all out neatly in a table or something. Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-907947 Share on other sites More sharing options...
sakibd2k Posted August 28, 2009 Author Share Posted August 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-908099 Share on other sites More sharing options...
F1Fan Posted August 28, 2009 Share Posted August 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172197-php-cant-print-all-the-rows-from-database/#findComment-908101 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.