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, 07: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, 07: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, 10:19 PM
lig's Avatar
lig lig is offline
"Forum Nazi"
Click here for more information.
 
Join Date: Apr 2007
Location: Jacksonville, Fl
Posts: 4,775 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 13 h 47 m 18 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, 09: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, 05: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, 10: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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




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