2011-08-01 40 views
-1

我工作的公司有一個Progress DB,可以存儲他們的大部分信息。他們要求我製作一個PHP腳本,可以從中獲取數據並將其與MySQL數據庫中的數據合併。當ODBC在同一個腳本中時,MySQL不工作

起初,我想我只是獲取數據,但過了一段時間後,我發現Progress DB的速度非常慢。我決定從MySQL或Progress取得頁面取決於哪個(MySQL取得進展)

我遇到了一個問題,儘管由於某些原因,當ODBC和MySQL似乎無法運行時既開放。我該如何解決這個問題?是否有可能做我需要它做的事情?

注意:我在各處都報錯,MySQL從未返回錯誤。該ODBC總是去和返回的內容,但它從來沒有將其插入MySQL數據庫

這裏是我的代碼:

$job_num = "59505"; 
$fields = 'JobNum, Name, City, State, StartDate, ReqDueDate'; 
$field_queries = 'j.JobNum AS JobNum, Name, City, State, jh.StartDate AS StartDate, ReqDueDate'; 

//Determine if there is a record in the MySQL DB that has the job 
$mysqlr = mysql_query("SELECT * FROM jobsinfo WHERE JobNum='$job_num'"); 
if(!$mysqlr){ 
    die(mysql_error()); 
} 
//If there is a record, display it from there: faster 
if(mysql_num_rows($mysqlr) > 0){ 
    //Take the fields and explode them into an array so that it can be looped through. 
    $field_array = explode(', ', $fields); 
    //Return each row from the database 
    while($row = mysql_fetch_array($mysqlr)){ 
     //Return all fields in the array 
     foreach($field_array as $key=>$field){ 
      echo $field .": ".$row[$field]."<br>"; 
     } 
     //Because the Description comes from a different part of the Progress include it here. 
     echo "Description:<br>".$row['Description']; 
    } 
}else{ 
    //If there is no record in the MySQL display it from the Progress AND copy it over. 
    //Begin by inserting a record to later be modified 
    mysql_query("INSERT INTO jobsinfo (JobNum) VALUES ('$job_num')") or die(mysql_error()); 
     $id = mysql_insert_id(); 
    //Connect to the Progress DB 
    $conodbc = odbc_connect($dsn, $username, $password, SQL_CUR_USE_ODBC); 
    //Explode the fields so that they can be looped through. 
    $field_array = explode(', ', $fields); 
    //Make the query to the Progress DB. Merge many tables into one query using JOINs 
    $sql = "SELECT TOP 1 ".$field_queries." FROM PUB.JobProd j LEFT JOIN PUB.BookOrd b ON j.OrderNum=b.OrderNum LEFT JOIN PUB.Customer c ON b.CustNum=c.CustNum LEFT JOIN PUB.JobHead jh ON j.JobNum=jh.JobNum WHERE j.JobNum = '$job_num' ORDER BY ReqDueDate DESC"; 
     //Execute the query 
     $rs = odbc_exec($conodbc,$sql) or die('Select failed!'); 
      //For each record loop through 
      while(odbc_fetch_row($rs)){ 
       //For each field display 
       foreach($field_array as $key=>$field){ 
        $value = odbc_result($rs, $field); 
        echo $field.": ".$value."<br>"; 
        //Update the previously inserted row with the correct information 
        mysql_query("UPDATE jobsinfo SET ".$field."='$value' WHERE id = '$id'"); 
       } 
      } 
    //Because there are multiple job parts it is easiest to just loop through it seperately and not JOIN it 
    $sql_asmbl = "SELECT * FROM PUB.JobAsmbl AS ja WHERE JobNum = '$job_num'"; 
     //Execture 
     $rs_asmbl = odbc_exec($conodbc,$sql_asmbl) or die('Select failed!'); 
     echo 'Description:<br>'; 
      $ptdesc =''; 
      //Loop through all the rows that match the job number 
      while(odbc_fetch_row($rs_asmbl)){ 
       $ptdesc .= odbc_result($rs_asmbl, 'PartNum') ." - "; 
       $ptdesc .= odbc_result($rs_asmbl, 'Description') ."<br>"; 
      } 
      $ptdesc = mysql_real_escape_string($ptdesc); 
      //Update the MySQL 
      mysql_query("UPDATE jobsinfo SET Description = '$ptdesc' WHERE id = '$id'"); 
      //Display it 
      echo $ptdesc; 
    //Close DB's 
    odbc_close($conodbc); 
    mysql_close($conn); 
} 
+0

此代碼是難以辨認的。你有沒有考慮寫文檔評論? –

+0

那裏你去找夥伴 – Chris

+0

那些沒有記錄評論。它們是逐行翻譯成英文的;這不是什麼意見。 (雖然這比原來稍微好一些,但是現在有一些代碼分組,並且在某些地方有理由。) –

回答

1

我看到你做的ODBC連接,但我沒有看到的mysql_connect( )或類似的使用mysqli或PDO。你真的打開一個套接字連接到MySQL,你剛剛離開這個代碼示例,或者你忘了在你的代碼中建立連接?

+0

我有一個配置文件包括,它只是用ODBC憑證斬下來的。看起來像這樣:'include('config/config.php');' – Chris

+0

如果odbc連接速度非常慢,那麼你的套接字連接到mysql可能會超時或腳本本身可能超時。更改php.ini中的腳本執行時間限制或my.cnf中的mysql連接超時值。 – 2011-08-01 17:20:01

+0

腳本執行時間設置爲5分鐘。相同的錯誤和沒有超時錯誤信息 – Chris

3

你假設的MySQL查詢總是成功運行:

$mysql = mysql_query("SELECT * FROM jobsinfo WHERE JobNum='$job_num'"); 

if(mysql_num_rows($mysql) > 0){ 
} 

你應該始終明確測試:

$mysql = mysql_query("SELECT * FROM jobsinfo WHERE JobNum='$job_num'") 
if(!$mysql){ 
    die(mysql_error()); 
} 
+0

沒有錯誤出現。它每次成功運行,它只是運行任何MySQL插入或更新 – Chris

+0

也許你正在使用InnoDB並禁用自動提交:http://dev.mysql.com/doc/refman/5.5/en/commit.html –

+0

這是一個MyISAM MySQL數據庫 – Chris

0

我感動的INSERT了一下,去掉了「來自(」 JobNum '),現在它工作正常。

找到錯誤的原因。 MySQL和ODBC都使用$ conn作爲它們的連接變量。這是造成錯誤。

+0

然後我堅持我的回答。如果您使用ODBC連接資源提供mysql函數,則它們應返回適當的錯誤值。但你必須檢查它。 –

相關問題