1

I have to query SQL-SERVER's Query and MYSQL's Query (That have the same result format but it's difference database) , But I want to call them in one while loop in PHP, Can I do that?

Ex.

$mssql_query = mssql_query(..mssql..);
$mysql_query = mysql_query(..mysql..);
while( $mssql_rs = mssql_fetch_assoc($mssql_query)|| $mysql_sql = mysql_fetch_assoc($mysql_query) )
{               
    ..some action..
}

EDITED : I just have an solved answer. It's work for me Idea from @Drew while(bContinue)

this my code

$mssql_query = mssql_query(..mssql..);
$mysql_query = mysql_query(..mysql..);
$continue = true;

while( $continue ){

    $ms_rs = mssql_fetch_assoc($mssql_query);
    $my_rs = mysql_fetch_assoc($mysql_query);

    if($ms_rs){ ..some action.. }
    if($my_rs ){ ..some action.. }

    $continue = $ms_rs|| $my_rs;

}

Thank you for all ideas.

paocom
  • 235
  • 1
  • 3
  • 8
  • I don't think this would work, because if the SQL Server fetch returns true, then the MySQL fetch won't even happen. – Tim Biegeleisen Sep 27 '16 at 04:49
  • I am not sure but instead of `||` you need to check through `&&` . – Alive to die - Anant Sep 27 '16 at 04:53
  • @Anant I try both `&&`, `||` but doesn't work" – paocom Sep 27 '16 at 04:58
  • @TimBiegeleisen I think so but maybe has another way, I'll find out – paocom Sep 27 '16 at 05:02
  • I think if you are doing any comparison inside the loop then, you should fetch data separately and compare through while loop but, it will increase the number of loops...It might be a way try this one – Ashish Ranade Sep 27 '16 at 05:04
  • its possible, the problem is if they don't match in terms of number of rows. `mssql rows 10` and `mysql rows 13`, why not just fetch them both separately then join the arrays – Kevin Sep 27 '16 at 05:05
  • 1
    `while(bContinue)` inside that do the queries followed by an`if` block. Deal with the ending of the `while` and the maintenance of the `bContinue` var – Drew Sep 27 '16 at 05:07
  • @Ghost I think he is considering same number of result. "That have the same result format but it's difference database" – Ashish Ranade Sep 27 '16 at 05:08
  • @paocom you can't do like that but you can write multiple `select query` in one query and `execute` that query. – Ramesh Kumar Sep 27 '16 at 05:10
  • FYI, you are using same variable $mysql_query for both queries, and only one time fetch query is possible on one result, please refer: http://stackoverflow.com/questions/9913366/using-mysql-fetch-assoc-twice-in-a-single-php-script –  Sep 27 '16 at 05:15
  • @RameshKumar Sorry my wrong type, I've edited it. – paocom Sep 27 '16 at 05:16
  • 2
    **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman Sep 27 '16 at 05:29
  • If you switch to PDO you can use one interface for both databases. This is probably a really good idea to do now before you get too married to this obsolete code. – tadman Sep 27 '16 at 05:29
  • Thank you for all technic and idea, I has solved this : http://stackoverflow.com/a/39716726/3774804 – paocom Sep 27 '16 at 05:43

2 Answers2

1

You can shoot in the dark and try to use both data_seek flavors of each, if they have both the same number of rows and same column names.

Here's the idea / here's some pseudo code too (untested of course):

$mssql_query = mssql_query(< the mssql query statement >);
$mysql_query = mysql_query(< the mysql query statement >);

$ms_num_rows = mssql_num_rows($mssql_query);
$my_num_rows = mssql_num_rows($mysql_query);

if($ms_num_rows === $my_num_rows) { // same number of rows on both result sets

    for($i = 0; $i < $my_num_rows; $i++) {
        mssql_data_seek($mssql_query, $i);
        mysql_data_seek($mysql_query, $i);

        $ms_row = mysql_fetch_assoc($mssql_query);
        $my_row = mysql_fetch_assoc($mysql_query);

        echo $ms_row['same_field_1'];
        echo $my_row['same_field_1'];

        // and others
    }

}

Note: Of course you might get deprecation warnings for using these functions, most likely these functions have their PDO counterparts and you could port them into using those instead.

Manual entries:

http://php.net/manual/en/function.mssql-data-seek.php
http://php.net/manual/en/function.mysql-data-seek.php

Kevin
  • 41,694
  • 12
  • 53
  • 70
0

I just had an answer. It's working for me Idea from @Drew while($continue)

Here is my code

$mssql_query = mssql_query(..mssql..);
$mysql_query = mysql_query(..mysql..);
$continue = true;

while( $continue )

{               
    $ms_rs = mssql_fetch_assoc($mssql_query);
    $my_rs = mysql_fetch_assoc($mysql_query);
                
    if($ms_rs){ ..some action.. }
    if($my_rs ){ ..some action.. }

    $continue = $ms_rs|| $my_rs;

}

Thank you for all the ideas.

Khadija
  • 310
  • 1
  • 9
paocom
  • 235
  • 1
  • 3
  • 8