PEAR Packages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPEAR Packages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old October 12th, 2005, 08:38 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
fetchRow

I am programming a function to transfer data from one database to another. I am using pear (and mysql is database).

The problem is this:
I don't know the fields are before they are transferred. So before I can run a SQL UPDATE query I must get the fields from another table.

I thought about a nested query at first but wasn't sure how to formulate it so I was trying a workaround:

// MySQL specific - change if another database - CONCAT_WS
$mysql="SELECT CONCAT_WS( 'X', sid, gid, qid ) FROM survey_data_questions WHERE sid='".$surveyid."'";

$result = $db_object->query($mysql);

$counter = 0;
$sqld = "SELECT";
while ($row = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
$counter = $counter++;
$field = $row->$counter;
$sqld = $sqld." ".$field;
}

---
Once I have all the fields in select query (yes, I know I still need to put in commas as separators), I wanted to use the same/similar string to update the table with the actual data. (e.g. $sql = "UPDATE ".$newtable." SET ".$field1." = ".$field1data.", " ...) except generated dynamically.

I don't seem to be able to get the DB_FETCHMODE_ORDERED working correctly with a counter incrementing it but don't know how to get the data out of that array any other way.

Any suggestions would be greatly appreciated.

Additionally, if there is a better way to do it rather than having all these separate queries (nested), that would save me more time and reduce server resources.

Thank you!

Reply With Quote
  #2  
Old October 12th, 2005, 08:52 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: fetchRow

After consulting some reference materials I found the correct syntax for getting the information out of the $row array:

$row[$counter]

My counter, however is still not incrementing. (have even changed it to $counter = $counter + 1;) but I still only have the first record. (where $counter=0)

Any thoughts?

Reply With Quote
  #3  
Old October 12th, 2005, 11:19 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Codewalkers Demi-God (4500 - 4999 posts)
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,753 lig User rank is Private First Class (20 - 50 Reputation Level)lig User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 8 h 15 m 54 sec
Reputation Power: 7
RE: fetchRow

First - have you considered using tableInfo() to get your column names?

As for a better way - maybe use mysqldump and mysqlimport - See http://dev.mysql.com/doc/refman/5.0/en/upgrading-to-arch.html

why use PHP if the DB itself can be used to do it.

please post your new code for futher help on that line.

Reply With Quote
  #4  
Old October 13th, 2005, 10:57 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: fetchRow

Thank you for the information about the tableinfo function. I may use it in the future. (although I think I'll have to massage the data a little...)

I was able to use the following code to view the table information:

$info = $db_object->tableInfo($surveydatatable);
print_r($info);

---
Regarding the "why" for what I'm doing. I don't want to get married to any specific technology. (that's why I'm using pear in the first place.) You can see I already use $mysql variables in my code for non-portable sections. (specific to mysql) Moreover from the pear website:

tableInfo() is not portable because not all drivers have this method, many DBMS's are unable to determine table names from query results and the metadata returned by each database system varies dramatically.

Only fbsql and mysql provide full information from queries.

---

Regardless I was wrong in one thing - (I had the table fields listed in another table) Here is the code I plan to use to extract the field info... I suppose the $counter variable I was using was reserved or something. The "$i" seems to work quite well.

$result = $db_object->query($mysql);

$i = 0;
while ($row = $result->fetchRow(DB_FETCHMODE_ORDERED)) {
$sqld = $sqld.$row[$i].", ";
$i = $i++;
}

echo $sqld;

Basically I loop through the results and append them to a string. Then I can use the string to pull the data and insert the data into table number 2.


Reply With Quote
  #5  
Old October 14th, 2005, 06:33 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: fetchRow

It seems my solution didn't work because table I was querying (a list of fields in another table) did not have all the fields. I reverted back to the tableinfo function and it worked. (Still dependent on MySQL, however) Anyway I have the function below if anyone wants to find the field name of a table before querying from that table. (Use whatever db connect method you wish)

You may wish to know why if a simple "SELECT * ..." would suffice later. With this loop, you can weed out the fields you don't want to transfer or even format them however you wish. (In the loop just put an if != $variable statement) This really helps if you don't know what fields you want to transfer exactly but you know how they should be formatted.

function gettablefields($dbname,$table){
// Get field names

require('admin/dbaccess.php'); // partial database connect script.
$datasource = $datasource."/".$dbname;
$db_object = DB::connect($datasource, TRUE);
if(DB::isError($db_object)) {
die($db_object->getMessage());
}

$info = $db_object->tableInfo($table);

foreach($info as $fieldname) {
$sqld = $sqld.", ".$fieldname['name'];
}
}
return $sqld;
}


Hopefully I haven't confused people more than I helped!

Duane

Reply With Quote
  #6  
Old October 14th, 2005, 11:42 PM
drinehart drinehart is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 20 drinehart User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: fetchRow

Another question...

I now have a $sql string (SELECT a, b, c, etc. FROM ...)

I pass the string to a function and want to put the results in another string, delimited by commas. I was trying to use the same type of format as before but this time I have no luck. I don't really have enough experience with the pear technology to get it going. Can someone tell me what the code should be?

function a($sql){

$result2 = $db_object->query($sql);
$i = 0;

while ($row = $result2->fetchRow(DB_FETCHMODE_ORDERED)) {
echo $row[$i];
cleanfordb($row[$i]);
$sqld1 = $sqld1.", '".$row[$i]."'";
$i = $i + 1;
}
return $sqld1; //This should be the result, delimited by commas.
}

Thanks for any help someone can provide.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPEAR Packages > fetchRow


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek