ODBC Query Results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Poe

    ODBC Query Results

    Hi,

    Consider the following:

    <?

    $username = "foo";
    $password = "bar";
    $host = "db";

    $connect = odbc_connect("$ host", "$username" , "$password" ) or die;

    $query = "SELECT id, name, address, city, state, comments1, comments2
    FROM data.table1, data.table2 WHERE id = '795'";

    $result = odbc_exec($conn ect, $query);

    while(odbc_fetc h_row($result)) {
    $id = odbc_result($re sult, 1);
    $name = odbc_result($re sult, 2);
    $address = odbc_result($re sult, 3);
    $city = odbc_result($re sult, 4);
    $state = odbc_result($re sult, 5);
    $zip = odbc_result($re sult, 6);
    $comments1 = odbc_result($re sult, 7);
    $comments2 = odbc_result($re sult, 8);
    print("ID: $id<br>Name: $name<br>Addres s: $address<br>Cit y:
    $city<br>State: $state<br>Zip: $zip<br>Comment s1:
    $comments1<br>C omments2: $comments2<br>\ n");

    }
    ?>

    Only one record is returned from data.table1 (containing the id, name,
    address, city, state, & zip). However, there are multiple records
    returned from data.table2 (records from comments1 & comments2). What
    is happening is that when I run this script, all of the data returned
    from data.table1 is printed over & over again along with each record
    returned from data.table2. How can I make it such that id, name,
    address, city, state & zip are only printed once - and allow comments1
    & comments2 to be printed multiple times for each occurance in the
    database?

    I've thought about just creating a second database query for the
    records in data.table2, however I need the join I'm getting by using
    the existing query.

    For what it's worth, I'm connecting to a DB2 database using PHP /
    Apache on Linux.

    I'm confused by this. Any help appreciated.

    Thanks.

  • Brent Palmer

    #2
    Re: ODBC Query Results


    "Mike Poe" <trolling4dolla rs@gmail.com> wrote in message
    news:1111005712 .141161.137350@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi,
    >
    > Consider the following:
    >
    > <?
    >
    > $username = "foo";
    > $password = "bar";
    > $host = "db";
    >
    > $connect = odbc_connect("$ host", "$username" , "$password" ) or die;
    >
    > $query = "SELECT id, name, address, city, state, comments1, comments2
    > FROM data.table1, data.table2 WHERE id = '795'";
    >
    > $result = odbc_exec($conn ect, $query);
    >
    > while(odbc_fetc h_row($result)) {
    > $id = odbc_result($re sult, 1);
    > $name = odbc_result($re sult, 2);
    > $address = odbc_result($re sult, 3);
    > $city = odbc_result($re sult, 4);
    > $state = odbc_result($re sult, 5);
    > $zip = odbc_result($re sult, 6);
    > $comments1 = odbc_result($re sult, 7);
    > $comments2 = odbc_result($re sult, 8);
    > print("ID: $id<br>Name: $name<br>Addres s: $address<br>Cit y:
    > $city<br>State: $state<br>Zip: $zip<br>Comment s1:
    > $comments1<br>C omments2: $comments2<br>\ n");
    >
    > }
    > ?>
    >
    > Only one record is returned from data.table1 (containing the id, name,
    > address, city, state, & zip). However, there are multiple records
    > returned from data.table2 (records from comments1 & comments2). What
    > is happening is that when I run this script, all of the data returned
    > from data.table1 is printed over & over again along with each record
    > returned from data.table2. How can I make it such that id, name,
    > address, city, state & zip are only printed once - and allow comments1
    > & comments2 to be printed multiple times for each occurance in the
    > database?
    >
    > I've thought about just creating a second database query for the
    > records in data.table2, however I need the join I'm getting by using
    > the existing query.
    >
    > For what it's worth, I'm connecting to a DB2 database using PHP /
    > Apache on Linux.
    >
    > I'm confused by this. Any help appreciated.
    >
    > Thanks.
    >[/color]

    If the var $id is unique then you could use,
    $result = odbc_exec($conn ect, $query);

    while(odbc_fetc h_row($result)) {
    $id = odbc_result($re sult, 1);
    $name = odbc_result($re sult, 2);
    $address = odbc_result($re sult, 3);
    $city = odbc_result($re sult, 4);
    $state = odbc_result($re sult, 5);
    $zip = odbc_result($re sult, 6);
    $comments1 = odbc_result($re sult, 7);
    $comments2 = odbc_result($re sult, 8);
    if ($id == $last_id){
    print("Comments 1:$comments1<br >Comments2: $comments2<br>\ n");
    }
    else {
    print("ID: $id<br>Name: $name<br>Addres s:
    $address<br>Cit y:$city<br>Stat e: $state<br>Zip:
    $zip<br>Comment s1:$comments1<b r>Comments2: $comments2<br>\ n");
    }
    $last_id = $id;

    }

    Brent Palmer.


    Comment

    • Mike Poe

      #3
      Re: ODBC Query Results


      Brent Palmer wrote:[color=blue]
      > "Mike Poe" <trolling4dolla rs@gmail.com> wrote in message
      > news:1111005712 .141161.137350@ g14g2000cwa.goo glegroups.com.. .[color=green]
      > > Hi,
      > >
      > > Consider the following:
      > >
      > > <?
      > >
      > > $username = "foo";
      > > $password = "bar";
      > > $host = "db";
      > >
      > > $connect = odbc_connect("$ host", "$username" , "$password" ) or die;
      > >
      > > $query = "SELECT id, name, address, city, state, comments1,[/color][/color]
      comments2[color=blue][color=green]
      > > FROM data.table1, data.table2 WHERE id = '795'";
      > >
      > > $result = odbc_exec($conn ect, $query);
      > >
      > > while(odbc_fetc h_row($result)) {
      > > $id = odbc_result($re sult, 1);
      > > $name = odbc_result($re sult, 2);
      > > $address = odbc_result($re sult, 3);
      > > $city = odbc_result($re sult, 4);
      > > $state = odbc_result($re sult, 5);
      > > $zip = odbc_result($re sult, 6);
      > > $comments1 = odbc_result($re sult, 7);
      > > $comments2 = odbc_result($re sult, 8);
      > > print("ID: $id<br>Name: $name<br>Addres s:[/color][/color]
      $address<br>Cit y:[color=blue][color=green]
      > > $city<br>State: $state<br>Zip: $zip<br>Comment s1:
      > > $comments1<br>C omments2: $comments2<br>\ n");
      > >
      > > }
      > > ?>
      > >
      > > Only one record is returned from data.table1 (containing the id,[/color][/color]
      name,[color=blue][color=green]
      > > address, city, state, & zip). However, there are multiple records
      > > returned from data.table2 (records from comments1 & comments2).[/color][/color]
      What[color=blue][color=green]
      > > is happening is that when I run this script, all of the data[/color][/color]
      returned[color=blue][color=green]
      > > from data.table1 is printed over & over again along with each[/color][/color]
      record[color=blue][color=green]
      > > returned from data.table2. How can I make it such that id, name,
      > > address, city, state & zip are only printed once - and allow[/color][/color]
      comments1[color=blue][color=green]
      > > & comments2 to be printed multiple times for each occurance in the
      > > database?
      > >
      > > I've thought about just creating a second database query for the
      > > records in data.table2, however I need the join I'm getting by[/color][/color]
      using[color=blue][color=green]
      > > the existing query.
      > >
      > > For what it's worth, I'm connecting to a DB2 database using PHP /
      > > Apache on Linux.
      > >
      > > I'm confused by this. Any help appreciated.
      > >
      > > Thanks.
      > >[/color]
      >
      > If the var $id is unique then you could use,
      > $result = odbc_exec($conn ect, $query);
      >
      > while(odbc_fetc h_row($result)) {
      > $id = odbc_result($re sult, 1);
      > $name = odbc_result($re sult, 2);
      > $address = odbc_result($re sult, 3);
      > $city = odbc_result($re sult, 4);
      > $state = odbc_result($re sult, 5);
      > $zip = odbc_result($re sult, 6);
      > $comments1 = odbc_result($re sult, 7);
      > $comments2 = odbc_result($re sult, 8);
      > if ($id == $last_id){
      > print("Comments 1:$comments1<br >Comments2:[/color]
      $comments2<br>\ n");[color=blue]
      > }
      > else {
      > print("ID: $id<br>Name: $name<br>Addres s:
      > $address<br>Cit y:$city<br>Stat e: $state<br>Zip:
      > $zip<br>Comment s1:$comments1<b r>Comments2: $comments2<br>\ n");
      > }
      > $last_id = $id;
      >
      > }
      >
      > Brent Palmer.[/color]

      Great Brent, thanks! That seems to work beautifully.

      Comment

      Working...