我工作的一家公司有一个Progress DB,存储了很多他们的信息。他们让我制作一个PHP脚本,可以从中提取数据并将其与MySQL数据库中的数据合并。
一开始我想我只需要获取数据,但过了一会儿我发现进度数据库非常慢。我决定从MySQL或Progress中获取页面,这取决于哪个有它(MySQL胜过Progress)
我遇到了一个问题,虽然由于某种原因ODBC和MySQL似乎不能在打开时起作用。我怎么解决这个问题?它有可能做我需要它做的事情吗?
注意:我在所有地方都抛出了错误捕获,MySQL从来没有返回一个错误。ODBC总是去并返回内容,但它从不将其插入MySQL DB
下面是我的代码:$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);
}
您假设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());
}
我看到您创建了ODBC连接,但我没有看到mysql_connect()或使用mysqli或PDO的类似内容。你是否打开了一个到mysql的套接字连接只是在这个代码示例中遗漏了这个还是你忘记在代码中建立连接了?
我将INSERT向上移动了一点,并删除了' from ('JobNum'),现在它可以正常工作了。
找到错误原因。MySQL和ODBC都使用$conn作为它们的连接变量。