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.

14
  • I don't think this would work, because if the SQL Server fetch returns true, then the MySQL fetch won't even happen. Commented Sep 27, 2016 at 4:49
  • I am not sure but instead of || you need to check through && . Commented Sep 27, 2016 at 4:53
  • @Anant I try both &&, || but doesn't work" Commented Sep 27, 2016 at 4:58
  • 1
    while(bContinue) inside that do the queries followed by anif block. Deal with the ending of the while and the maintenance of the bContinue var Commented Sep 27, 2016 at 5:07
  • 2
    WARNING: If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and has been removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way helps explain best practices. Make sure your user parameters are properly escaped or you will end up with severe SQL injection bugs. Commented Sep 27, 2016 at 5:29

2 Answers 2

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

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

Comments

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.

Comments

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.