MySQL not working when ODBC is in the same script
A company I am working for has a Progress DB that store much of their information. They asked me to make a PHP script that can pull data from it and merge it with data inside of a MySQL database.
At first I figured I would just fetch the data, but after a while I found that the Progress DB was incredibly slow. I decided to have the page fetch from either MySQL or Progress depending on which had it (MySQL trumping Progress)
I ran into a problem though in that for some reason ODBC and MySQL don't seem to be able to function when both open. How can I solve this? Is it possible to do what I am needing it to do?
Note: I threw catches for errors all over the place and MySQL never returned an error. The ODBC always goes and returns the content, but it never INSERTs it into the MySQL DB
Here is my code:
$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('S开发者_C百科elect 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);
}
You are assuming that MySQL queries always run successfully:
$mysql = mysql_query("SELECT * FROM jobsinfo WHERE JobNum='$job_num'");
if(mysql_num_rows($mysql) > 0){
}
You should always test it explicitly:
$mysql = mysql_query("SELECT * FROM jobsinfo WHERE JobNum='$job_num'")
if( !$mysql ){
die(mysql_error());
}
I see that you make an ODBC connection, but I do not see mysql_connect() or something similar using mysqli or PDO. Are you actually opening a socket connection to mysql and you just left that out of this code example or did you forget to make the connection in your code?
I moved the INSERT up a bit and removed the ' from ('JobNum') and now it works fine.
Found the cause of the error. Both the MySQL and ODBC were using $conn as their connecting variable. This was causing errors.
精彩评论