473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Jul 17 '05 #1
4 2200
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 =--
Jul 17 '05 #2
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 <he****@hotpop.com> wrote in message news:<bq*************@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);

Jul 17 '05 #3
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 =--
Jul 17 '05 #4
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 <he****@hotpop.com> wrote in message news:<bq*************@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];
// ...
?>

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
1
by: Jon LaRosa | last post by:
Hi all - I have a web application and I want to be able to do some basic error handling. For example, here is one error I would like to catch and display in a useful way for the user: ...
0
by: Jon LaRosa | last post by:
Hi all - I have a web application and I want to be able to do some basic error handling. For example, here is one error I would like to catch and display in a useful way for the user:...
4
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow...
4
by: kcddoorman | last post by:
Ok, I have almost masted the IF and IIF functions but these take up to many fields in a query. Suppose I have 10 different sales people in my database that want to receive an e-mail when their orders...
5
by: mctime | last post by:
Hello, I am attempting to split a raw data table into a new table that has split out a specific field in the raw data and created a new record for each split but I have come to an impasse due to...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
by: Josh | last post by:
I run a Joomla website and am familiar with php in some but not all aspects. Currently I am trying to find some solutions related to session handling. Am I correct in saying that "login" is kept...
4
by: hapnendad | last post by:
In the question statement below Field names are in and variables are in (). All fields referenced are in what I have named the ‘PAR’ Table. Using MS Access 2003, I am working on a project...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.