Page 1 of 1

Store an array in an array? PostgreSQL and PHP

Posted: Fri Oct 18, 2002 4:30 am
by catherine878
I am using PHP to access a PostgreSQL database for an online banking application. I am having some problems with arrays.

//assume database connectivity
$sql = "some SQL statement"
$result = pg_query ($dbconn, $sql);
$data = pg_fetch_row ($result);
$ans = Array ();

//in this while loop I want to put the rows that satisfied a condition into the array $ans

while ($data != false) {
if cond {
$ans[] = $data;
$data = pg_fetch_row ($result);
}
}

I am really not familiar with the array syntax and could not find any relevant examples. Can someone tell me how to store an array $data into another array $ans and later to retrieve the values? Further, is there a way to create the array $ans = Array () with a give array size like $size?

Thank you so much!!!

Posted: Fri Oct 18, 2002 9:19 am
by volka
your code should be working. A slightly shorter version

Code: Select all

//assume database connectivity 
$sql = "some SQL statement" 
$result = pg_query ($dbconn, $sql); 
$ans = Array (); 

//in this while loop I want to put the rows that satisfied a condition into the array $ans 
while ($data=pg_fetch_row ($result); )
{ 
	if (cond)
		$ansї] = $data; 
}
is there no way of putting the condition in the sql-statement?

you can iterate the array i.e. with foreach