Store an array in an array? PostgreSQL and PHP

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
catherine878
Forum Newbie
Posts: 6
Joined: Wed Jun 26, 2002 7:04 pm

Store an array in an array? PostgreSQL and PHP

Post 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!!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply