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
Solved

php and postgres quessie.....

Posted on 2002-06-02
6
260 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.

856 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