Solved

php and postgres quessie.....

Posted on 2002-06-02
6
269 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

733 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