Solved

php and postgres quessie.....

Posted on 2002-06-02
6
274 Views
Last Modified: 2006-11-17
i have an sql command like...

select account.* billing.* orders.* from account, billing, orders WHERE.....

and I am putting the results of this query into a serialized php array... like so.....

 for($i=0; $row = @pg_fetch_array($res,$i); $i++)
    {
        $tabledata = $row;
    }

however, i am getting undesired results because the tables have common column names, for example, accounts.id order.id and billing.id and yet my $tabledata variable just has the on $tabledata["id"] element...

what i need (and i am not sure if its at a php level or sql level) is something which will allow me to have an array indexed by tablename.columnname rather than just column name...

so for the above example i would have

$tabledata['accounts.id'] == //the id var from the account table
$tabledata['billing.id'] == // the id var from the billing table
$tabledata['orders.id'] == // the id var from the orders table

instead of just the one $tabledata['id'] value from who knows what table...

can anyone help!!??


0
Comment
Question by:n1875621
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:christopher sagayam
ID: 7050759
how about having something like

$tabledata[$tablename][$id];
0
 

Author Comment

by:n1875621
ID: 7050787
no... because the information need to be serialized into an array and I cant tell what "$tablename" is from the sql query...
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 7051004
In that case you should go for pg_fetch_row.

$result = pg_Exec($conn,"select account.*,billing.*,order.* from ....");

$num = pg_numrows($result);

for ($i=0; $i < $num; $i++)
{
  $r = pg_fetch_row($result, $i);

  for ($j=0; $j < count($r); $j++) {
    echo "$r[$j]&nbsp;";
}

// the fields would be in the same order as in table and between tables it would be in same order as you have wrote table names.

JD


0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:n1875621
ID: 7052339
thats fine, except i am not sure what tables are being selected and in what order... its a dynamic sql query....

any ideas?
0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7052649
What type of database are you using, MySQL, MSSQL, Oracle, ...?

I think the ideal solution would be to alias each of the fields, but it depends how the dynamic SQL is being built.  Can you give more details of this part of the code?

Generally speaking, unless you actually alias the fields in some way then the first "id" field will be "id" the next one "id_1" and so on.

If you can get you sql to look more like...

select
  account.id 'account.id',
  account.name 'account.name',
  billing.id 'billing.id',
  billing.address 'billing.address',
  orders.id 'orders.id'
  ...
from
  account, billing, orders
where.....

... then your problems will be solved.

Depending on your database being used, you can probably extract the necessary field names to build the SQL like this.
0
 

Author Comment

by:n1875621
ID: 7052657
that worked fine.... not exactly what i needed but the pricipal was there...

cheers
john
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question