0

I have php code with two queries. One is in an Oracle database, with the result being about 100 rows of data (within the data there are SKU numbers). The other is in a mysql database with about 30 rows of data (with the data there are also SKU numbers).

I need to compare each SKU in the first query to the second query, line by line. If the SKU in the first query also appears in the second query, then I need it to echo the SKU.

All SKUs in the second query are in the first query, so I would expect the result to echo all 30 SKUs, but it does not. Depending on how I change the syntax, it echos 17 rows, 100 rows, or no rows at all.

It's worth noting that both queries work fine. The Oracle query is insanely long and has a lot of sub-queries in it, so I will not include that full query below. The Oracle query returns the results perfectly in SQL Developer, and the MySQL query returns the results perfectly in HeidiSQL. Both queries have been tested and echoed as tables in other php files to make sure that they were working fine.

Below is what the php file that I am trying to fix looks like so far;

<?php

$conn = oci_connect($user, $pswd, $hst);
$sql = oci_parse($conn,"[Really long Oracle Query]");

while ($row = oci_fetch_assoc($sql))
{
    $sku = $row['SKU'];
    $con = mysql_connect("[database host]", "[database user]", "[database password]");
    mysql_select_db("[database_name]");
    $sqls = "SELECT * FROM [table_name] WHERE [this_date_column] BETWEEN 
        DATE_SUB(NOW(), INTERVAL 14 DAY) AND NOW()";
    $result = mysql_query($sqls);
    $mailer = NULL;

    while($rows = mysql_fetch_assoc($result))
    {
        $m_sku = $rows['sku'];
        if ($m_sku == $sku)
        {
            $mailer == 'false';
        }   
    }
    if ($mailer == 'false')
    {
        echo $m_sku;
        echo "<br>";
    }       
}

?>

Again; there are only 30 SKUs in the MySQL query, but over 100 SKUs in the Oracle query. All SKUs in the MySQL query are definitely in the Oracle query.

Anyone see what I am doing wrong?

Thanks in advance,

-Anthony

7
  • mysql connecting in while loop, you need to connect to it just one time not in every loop. Commented Apr 8, 2014 at 14:31
  • Even though I can't understand your question, suppose that if ($mailer == 'false') should be inside your second while as you want to check out $mailer. Also why don't you just use boolean instead of string "false" ! Commented Apr 8, 2014 at 14:32
  • @MarkBaker: No, that is just a typo to replace real info. I will correct. Commented Apr 8, 2014 at 14:34
  • $mailer == 'false'; should be $mailer = 'false'; or probably should really be $mailer = false; Commented Apr 8, 2014 at 14:35
  • 1
    Is this a legacy application? mysql_query shouldn't be used in new code because it's been deprecated, is dangerous if used incorrectly, and is being removed from future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way has a number of recommendations as to how to keep your application up-to-date. Commented Apr 8, 2014 at 14:52

2 Answers 2

1

You have basic sintacsis errors:
= is used to assign values
== compares 2 variables (not counsidering there type *)
=== compares 2 variables including there types.

your code should look something like this:

while($rows = mysql_fetch_assoc($result))
    {
        $m_sku = $rows['sku'];
        if ($m_sku == $sku)
        {
            $mailer = false; // == is for comparison, = is for assign
        }   
    }
    if ($mailer === false)
    {
        echo $m_sku;
        echo "<br>";
    }   

if($member == 'false'){...}

when 'false' is compared with == to a falsefull value (0, null, false, array(), '') .. it will NOT be mach it as it is parsed as a "not empty string" so it is not falsefull .

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the code above. It only gives me the first 21 SKUs that match in both queries. I cannot figure out why it will not match all 30 SKUs. This is a step in the right direction, but it is still not matching all 30 SKUs.
This was the closest answer that actually helped me to semi-solve this problem. Thanks!
0

Here is your code with the MySQL connect moved outside the loop and the == assignment fixed.

<?php

//Connect to databases
$oracle = oci_connect($user, $pswd, $hst);
$mysql = mysql_connect("[database host]", "[database user]", "[database password]");
mysql_select_db("[database_name]");

//Get rows from Oracle
$oracle_result = oci_parse($oracle, "[Really long Oracle Query]");
$oracle_rows = array();
while ($row = oci_fetch_assoc($oracle_result)) $oracle_rows[] = $row;

//Get rows from MySQL
$mysql_sql = "SELECT * FROM [database_name] WHERE this_date_column BETWEEN 
    DATE_SUB(NOW(), INTERVAL 14 DAY) AND NOW()";
$mysql_result = mysql_query($mysql_sql);
$mysql_rows = array();
while ($row = mysql_fetch_assoc($mysql_result)) $mysql_rows[] = $row;

foreach ($oracle_rows as $oracle_row) {
    $oracle_sku = $oracle_row['SKU']; 
    foreach ($mysql_rows as $mysql_row) {
        $mysql_sku = $mysql_row['sku'];
        $mailer = null;
        if ($mysql_sku == $oracle_sku) {
            $mailer = false;
            echo $mysql_sku . "<br>";
        }   
    }
}

4 Comments

Below is the error given when I replace my original code with the code above: Warning: oci_fetch_assoc(): ORA-24374: define not done before fetch or execute and fetch in /var/www/html/ugwarehouse/sku_test2.php on line 214 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /var/www/html/ugwarehouse/sku_test2.php on line 219
I think you usually get those errors when the query fails, did you replace all the [bracketed text] and set the $user, $pswd, $hst variables?
Yes, I replaced all of the bracketed text.
Is there a mysql equivalent to oci_parse? Maybe I need to use that function for the mysql query, if it exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.