What is causing this modx error?

142 views Asked by At

I have the strangest error, a simple import/export script in a modx site is causing some very strange behaviour. here is the script:

<?php
/**
 * Project: httpdocs
 * Created by: BigBlock Studios .
 * User: Sean Kimball
 * Date: 16/07/14
 * Time: 2:44 PM
 */

$remoteip = $_SERVER['REMOTE_ADDR'];

$allowedips = array('xxx.xxx.xxx.xxx','xxx.xxx.xxx.xxx');

if(!in_array($remoteip, $allowedips)){ echo 'denied'; return; }

error_reporting(E_ALL);
ini_set('display_errors', '1');

$mshost = 'xxx.xxx.xxx.xxx';
$msdb = 'msdb';
$msuser = 'msuser';
$mspass = 'mspass';

$myhost = 'xxx.xxx.xxx.xxx';
$mydb = 'mydb';
$myuser = 'myuser';
$mypass = 'mypass';
$mytable = 'modx_gssi_inventory_data';
$gatable = 'modx_gssi_inventory_gage';
$n = 100;

$query ="select TOP $n RECORDNUMBER,RESERVECUST,TAGNUMBER,ORIGINALPRICE,WIDTH,LENGTH,SCALEWGT,MINGAGE,IBLNUMBER,MILLTAGNO,VISUALDESC,FINISH,LOCATION,PONUMBER,VENDORNUMBER,STORAGETAGNO,PRODCLASS,RELEASENUMBER, PURCHASEDESC,NCTCT,STATUS,SALESDESC,CUSTOMERPONUMBER,PLANTLOCATION,LINEALFT,PIW from dbo.POInvWrk WHERE STATUS = 'A' AND RESERVECUST = ''";

$connection = mssql_pconnect($mshost, $msuser, $mspass);

if(!$connection){echo 'Could not connect to SQL server';}

if (!mssql_select_db($msdb, $connection)) {echo 'Unable to select MSSQL database!';}

$results = mssql_query($query, $connection);

// echo mssql_num_rows($results);

if (!mssql_num_rows($results)) {

    echo 'No MSSQL records found';

} else {

    $conn = mysql_connect($myhost, $myuser, $mypass);
    mysql_select_db($mydb, $conn);
    if (!$conn) { echo 'Failed to connect to MySQL'; }
    mysql_query("delete from $mytable")or die(mysql_error()."delete failed");

    while ($result = mssql_fetch_array($results, MSSQL_BOTH)) {

        $TAGNUMBER = trim(mysql_real_escape_string($result['TAGNUMBER']));
        $NCTCT = trim(mysql_real_escape_string($result['NCTCT']));
        $PRODCLASS = trim(mysql_real_escape_string($result['PRODCLASS']));
        $FINISH = trim(mysql_real_escape_string($result['FINISH']));
        $MINGAGE = trim(mysql_real_escape_string($result['MINGAGE']));
        $WIDTH = trim(mysql_real_escape_string($result['WIDTH']));
        $LENGTH = trim(mysql_real_escape_string($result['LENGTH']));
        $SCALEWGT = trim(mysql_real_escape_string($result['SCALEWGT']));
        $RESERVECUST = trim(mysql_real_escape_string($result['RESERVECUST']));
        $PONUMBER = trim(mysql_real_escape_string($result['PONUMBER']));
        $PLANTLOCATION = trim(mysql_real_escape_string($result['PLANTLOCATION']));
        $LOCATION = trim(mysql_real_escape_string($result['LOCATION']));
        $STORAGETAGNO = trim(mysql_real_escape_string($result['STORAGETAGNO']));
        $VENDORNUMBER = trim(mysql_real_escape_string($result['VENDORNUMBER']));
        $ORIGINALPRICE = trim(mysql_real_escape_string($result['ORIGINALPRICE']));
        $MILLTAGNO = trim(mysql_real_escape_string($result['MILLTAGNO']));
        $RELEASENUMBER = trim(mysql_real_escape_string($result['RELEASENUMBER']));
        $IBLNUMBER = trim(mysql_real_escape_string($result['IBLNUMBER']));
        $VISUALDESC = trim(mysql_real_escape_string($result['VISUALDESC']));
        $PURCHASEDESC = trim(mysql_real_escape_string($result['PURCHASEDESC']));
        $RECORDNUMBER = trim(mysql_real_escape_string($result['RECORDNUMBER']));
        $STATUS = trim(mysql_real_escape_string($result['STATUS']));
        $SALESDESC = trim(mysql_real_escape_string($result['SALESDESC']));
        $CUSTOMERPONUMBER = trim(mysql_real_escape_string($result['CUSTOMERPONUMBER']));
        $LINEALFT = trim(mysql_real_escape_string($result['LINEALFT']));
        $PIW = trim(mysql_real_escape_string($result['PIW']));

        $GA = mysql_query("select ga from $gatable where $MINGAGE >= min and $MINGAGE <= max;");
        echo "select ga from $gatable where $MINGAGE >= min and $MINGAGE <= max = $GA <br />";
        $timestamp = time();

        $insert = "insert into $mytable (`timestamp`,
                tag_no, classification, grade, finish,
                gage, ga, width, `length`,
                scale_wgt, cust, po_num, plant,
                location, storage_tag_no, vendor_number, original_price,
                mill_tag_no, release_number, ibl_number, visual_desc,
                purchase_desc, mssql_id, status, sales_desc,
                customer_po_num, nctct, linealft, piw)
            values (
                '$timestamp',
                '$TAGNUMBER',
                '$NCTCT',
                '$PRODCLASS',
                '$FINISH',
                '$MINGAGE',
                '$GA',
                '$WIDTH',
                '$LENGTH',
                '$SCALEWGT',
                '$RESERVECUST',
                '$PONUMBER',
                '$PLANTLOCATION',
                '$LOCATION',
                '$STORAGETAGNO',
                '$VENDORNUMBER',
                '$ORIGINALPRICE',
                '$MILLTAGNO',
                '$RELEASENUMBER',
                '$IBLNUMBER',
                '$VISUALDESC',
                '$PURCHASEDESC',
                '$RECORDNUMBER',
                '$STATUS',
                '$SALESDESC',
                '$CUSTOMERPONUMBER',
                '$NCTCT',
                '$LINEALFT',
                '$PIW'

        );";

        mysql_query($insert)or die(mysql_error()."insert failed".$insert);

    }

    //$mysqli->close();
}


?>

The problem was that it was not populating the "GA" field in the import, everything else runs fine.

The line where I echoed out the script selecting the "GA" value outputs records like this:

select ga from modx_gssi_inventory_gage where 0.182 >= min and 0.182 <= max = Resource id #10 

If I run the query against the modx_gssi_inventory_gage table, I get the expected result an integer not "Resource id #10"

  • notice that this script is not even aware of modx & should not be returning Resource Id anything.
  • it is run stand alone, nothing calls it just point a browser at the script.
  • I can run this from a domain that does not even have modx installed & it will still come back with this weirdness.

Any thoughts as to what is going on with this???

1

There are 1 answers

1
mts7 On BEST ANSWER

$GA = mysql_query("select ga from $gatable where $MINGAGE >= min and $MINGAGE <= max;");

That should return a resource, and it does. You need to process that result before you can use it efficiently.

$row = mysql_fetch_assoc($GA); $GA = $row['ga'];

See PHP's page about it.