Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with foreach function?

Status
Not open for further replies.

jisoo23

Programmer
Joined
Jan 27, 2004
Messages
192
Location
US
Hello all,

This may or may not be a PHP problem but it seems to be. I'm running PHP5 and Postgresql 7.4 on a Red Hat server. I'm making a database query (a rather complex one at that) which returns a recordset containing foreign key names. Here's the problem, when I run the query directly in the Postgresql database, the recordset returned looks fun. But when I run the same query in PHP, it returns the first foreign key twice. I.E. I toss the recordset into an array using pg_fetch_array, then use the foreach function to run through each item one at a time. But it ends up that I get a duplicate pair of the first entry. Has anyone seen this sort of error before? Perhaps it's a bug in PHP5?

Thanks,
Jisoo23
 
Can you post the code ?
Its difficult to guess from the above info.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Here is the actual SQL query:

Code:
SELECT pt.tgargs FROM pg_class pc, pg_proc pg_proc, pg_proc pg_proc_1, pg_trigger pg_trigger, pg_trigger pg_trigger_1, pg_proc pp, pg_trigger pt WHERE pt.tgrelid = pc.oid AND pp.oid = pt.tgfoid AND pg_trigger.tgconstrrelid = pc.oid AND pg_proc.oid = pg_trigger.tgfoid AND pg_trigger_1.tgfoid = pg_proc_1.oid AND pg_trigger_1.tgconstrrelid = pc.oid AND ((pc.relname= '<< TABLENAME >>>') AND (pp.proname LIKE '%%ins') AND (pg_proc.proname LIKE '%%upd') AND (pg_proc_1.proname LIKE '%%del') AND (pg_trigger.tgrelid=pt.tgconstrrelid) AND (pg_trigger_1.tgrelid = pt.tgconstrrelid));
where <<TABLENAME>> is the name of the table you want to pull foreign key names from.

So after that query I basically toss the record set into an array (using pg_fetch_array) and then using foreach, try to print each one out.

Code:
$foreign_keys = pg_fetch_array($recordset);
                foreach($foreign_keys as $foreign_key)
                {
                        $foreign_key = substr($foreign_key, 0, strpos($foreign_key, "\\"));
                        echo "FOREIGN_KEY = "'.$foreign_key.'";'."\n";
                        //echo "\t\t".'confrelid->"'.$foreign_key['confrelid'].'";'."\n";

                }

Any ideas are appreciated.

Thanks!
Jisoo23
 
What does the line $foreign_key['confrelid'] do. Is it the intrernal key like Ingres has a TId and Oracle has a RId. If it is do you not have to name it in the select columns in the query ?.
sorry for the stupid question but PG is not my db
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top