Handling case of field names in multiple db's

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Handling case of field names in multiple db's

    (Bottom line: I think what I'm looking for is an easy way of changing
    the case of key values in an array.)

    I've got code that I'm trying to make agnostic about the underlying
    database system I'm using. That is, I want the code to work whether
    the underlying db is MS SQL, MySQL, Oracle, etc. I'm using the PEAR DB
    package, and it's great for what I'm trying to do...except in one
    area. I get the returned row fields in an associative array, where the
    element key is the field name and the element value is the field
    value. The problem is that MS SQL returns field names in mixed case
    (actually, it returns the field names in the case the user used to
    define the field names in MS SQL in the first place), while Oracle
    returns the field names all caps. So, if I have the same db on MS SQL
    and on Oracle, I can't easily access the field values.
    For example:

    $row*=*$result->fetch_row(DB_FETCHMODE_ASSOC);

    Now, if I print_r($row), I see something like:

    Array*(
    ***[My_Id]*=*1;
    ***[Name]*=*"Fred Flintstone";
    )

    If I run this under an Oracle db, I might get this, instead:

    Array*(
    ***[MY_ID]*=*1;
    ***[NAME]*=*"Fred Flintstone";
    )

    So my code cannot say something such as:

    $id*=*$row["My_Id"];

    That would work for the MS SQL version, but not for the Oracle
    version. I certainly don't want to say something like:

    if*($IsMsSql)*$id*=*$row["My_Id"];
    else*$id*=*$row["MY_ID"];

    I really don't have control of how the users define the case of their
    field names, otherwise I could tell them to upper-case them all the
    time. But that might not work, if a different set of db routines that
    DB uses returns field names in all lower case.

    I could do the following, but I'm not sure I want to take the
    efficiency hit (maybe it isn't so bad; I don't know):

    foreach*($row*as*$key*=>*$value)*{
    ***$row*[strtoupper($key)]*=*$value;
    ***unset($row[$key]);
    }


    Any ideas? Thanks.
    Michael
    Michael Flanagan Guest

  2. Similar Questions and Discussions

    1. Xtension Dev - Multiple Recordset Field Names
      Anthony Brown wrote: Which extension is that? URL? I think that you have to go into the HTML file created by the SBB and add a third...
    2. #25205 [Com]: Class names case insensitiv
      ID: 25205 Comment by: reiersol at online dot no Reported By: thomas dot hebinck at digionline dot de Status: ...
    3. #25205 [Opn->Bgs]: Class names case insensitiv
      ID: 25205 Updated by: sniper@php.net Reported By: thomas dot hebinck at digionline dot de -Status: Open...
    4. #25205 [NEW]: Class names case insensitiv
      From: thomas dot hebinck at digionline dot de Operating system: Linux Debian Woody PHP version: 5CVS-2003-08-22 (dev) PHP Bug...
    5. Same field in two DB's
      I am using Access 2000 I have two different databases; One for material disposition (DB-1) and another one for Vendor returns (DB- 2). In the...
  3. #2

    Default Re: Handling case of field names in multiple db's

    Michael Flanagan wrote:
    > I could do the following, but I'm not sure I want to take the
    > efficiency hit (maybe it isn't so bad; I don't know):
    >
    > foreach ($row as $key => $value) {
    > $row [strtoupper($key)] = $value;
    > unset($row[$key]);
    > }
    Don't know about the efficiency stuff, but you don't want to go through
    that with $row['DATA'] !

    $row2 = array();
    foreach ($row as $key => $value) {
    $row2[strtoupper($key)] = $value;
    }
    $row = $row2;
    unset($row2);
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Graca Guest

  4. #3

    Default Re: Handling case of field names in multiple db's

    Good point; thanks.

    I've looked at constants, and so now I'm doing something like:

    define("USE_MSSQL", TRUE);

    if (defined("USE_MSSQL") && USE_MSSQL) {
    define("MY_ID", "My_Id");
    define("NAME", "Name");
    }
    else if (defined("USE_ORACLE") && USE_ORACLE) {
    define("MY_ID", "MY_ID");
    define("NAME", "NAME");
    }
    else {
    die("No db defined");
    }

    ....

    $id = $row[MY_ID];

    Still, am I missing something easier still?

    Michael

    Pedro Graca <hexkid@hotpop.com> wrote in message news:<bqdubh$21gvdd$1@ID-203069.news.uni-berlin.de>...
    > Michael Flanagan wrote:
    > > I could do the following, but I'm not sure I want to take the
    > > efficiency hit (maybe it isn't so bad; I don't know):
    > >
    > > foreach ($row as $key => $value) {
    > > $row [strtoupper($key)] = $value;
    > > unset($row[$key]);
    > > }
    >
    > Don't know about the efficiency stuff, but you don't want to go through
    > that with $row['DATA'] !
    >
    > $row2 = array();
    > foreach ($row as $key => $value) {
    > $row2[strtoupper($key)] = $value;
    > }
    > $row = $row2;
    > unset($row2);
    Michael Flanagan Guest

  5. #4

    Default Re: Handling case of field names in multiple db's

    Michael Flanagan wrote:
    > I've looked at constants, and so now I'm doing something like:
    [snip]
    > $id = $row[MY_ID];
    >
    > Still, am I missing something easier still?
    Well ... I've only ever used MySQL (and M$SQL for a while)
    and I never used mysql_fetch_assoc() (or mysql_fetch_array() -- UGH!).

    With mysql_fetch_row() (or mssql_fetch_row()) I get numerical
    indexes only, and I do:

    <?php
    // ...
    $id = $row[0];
    $name = $row[1];
    // ...
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Graca Guest

  6. #5

    Default Re: Handling case of field names in multiple db's

    Right. But then the problem becomes one of having to mod your code if
    you add a new field, or if the order of fields changes. I could
    mention each field by name in the SELECT statement, but...

    Thanks for your suggestions.

    Michael

    Pedro Graca <hexkid@hotpop.com> wrote in message news:<bqgon2$20mtur$1@ID-203069.news.uni-berlin.de>...
    > Michael Flanagan wrote:
    > > I've looked at constants, and so now I'm doing something like:
    > [snip]
    > > $id = $row[MY_ID];
    > >
    > > Still, am I missing something easier still?
    >
    > Well ... I've only ever used MySQL (and M$SQL for a while)
    > and I never used mysql_fetch_assoc() (or mysql_fetch_array() -- UGH!).
    >
    > With mysql_fetch_row() (or mssql_fetch_row()) I get numerical
    > indexes only, and I do:
    >
    > <?php
    > // ...
    > $id = $row[0];
    > $name = $row[1];
    > // ...
    > ?>
    Michael Flanagan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139