Combine 2 MySql results using array on PHP

Main theme of this article is, searching values in 2 different MySql queries and combining both results as array using array_combine() and array_push().

Explanation is given under source code.

<?php
//connection parameters
$username = “root”;
$password = “”;
$dbhost = “localhost”;
$database = “test”;

//Array creation
$table1array = array();
$table2array = array();
$finalarray = array();

//connect to database server
$dbconnection = mysql_connect($dbhost, $username, $password) or die(“Could not connect to MySQL Database”);
echo “Connection Success!!!”;

//connect to database
$dbhandle = mysql_select_db($database, $dbconnection) or die(“Could not select the test database”);

//Queries to combine
$t1 = “select * from `table1`”;
$t2 = “select * from `table2`”;

$table1query = mysql_query($t1);
$table2query = mysql_query($t2);

//Count the number of records are equal.
$t1_count = mysql_num_rows($table1query);
$t2_count = mysql_num_rows($table2query);

//If not equal, error will be produced
if($t1_count != $t2_count) {

echo ” But, both parameters should have an equal number of elements”;
exit;

}

//First table query to push, first table values into array by using array_push
while($table1row = mysql_fetch_array($table1query)) {

$table1value = $table1row['table1_value'];
array_push($table1array, $table1value);

}

//Second table query to push, second table values into array by using array_push
while($table2row = mysql_fetch_array($table2query)) {

$table2value = $table2row['table2_value'];
array_push($table2array, $table2value);

}

//Combine above generated arrays and combine into one
$finalarray = array_combine($table1array, $table2array);

//Print the output
print_r($finalarray); exit;

?>

EXPLANATION:

Create tables:

Connect to database with database username and password. Create database called “test” followed by creating 2 tables. Name those tables as ‘table1’ and ‘table2’. Inside both tables create 2 fields with names ‘t1_id’, ‘table1_value’ -> for table1 and ‘t2_id’, ‘table2_value’ -> for table2. Then insert values into 2 tables in fields ‘table1_value’ and ‘table2_value’ equally. Remember that, both tables’ values should be equal.

Create 3 arrays:
Return to PHP source code, Create 3 arrays using array() in names $table1array, $table2array, $finalarray. $table1array is to hold values from table 1 and $table2array is to hold values from table 2. $finalarray is to combine arrays $table1array and $table2array.

Execute 2 queries and compare:

$t1 and $t2 are MySql queries to combine values. Execute the 2 tables’ using mysql_query(). Count number of rows using mysql_num_rows(); Because values of 2 tables should be same, otherwise it will returns warning message.[ This is validated on next line 32 using if statement].

Push values into array:

If both table 1 and table 2 values are equal, it will run 40th line. There we using while loop to get table1 values into array by name $table1array using array_push(). Now all values of table 1 are moved to $table1array. Next line 48th, having another while loop, It’s also get table 2 values into array by name $table2array using array_push().

Combine 2 arrays:

We got 2 arrays which holding 2 different set of values. Combine, 2 arrays $table1array and $table2array by using array_push(). And named final array  $finalarray. Print the result print_r($finalarray);. That’s all. We achieved.

Final result:

If 2 tables (table1 and table 2) values are not equal. Output will be,

Connection Success!!! But, both parameters should have an equal number of elements

Otherwise output will look like [data can be differ as per ur input],

Connection Success!!!Array ( [1] => vijay [2] => jana [3] => selvaraj [4] => welcome [5] => hello world )

Download Source code:

http://www.splendidwebtech.com/code/download.php?file=CombineMySqlresults.zip

http://rapidshare.com/files/280269160/CombineMySqlresults.zip.html

You can leave a response, or trackback from your own site.

4 Responses to “Combine 2 MySql results using array on PHP”

  1. Karthikeyan says:

    Mr. Vijayakumar, Nice blog. Don’t carry your site in a narrow way. Please, concentrate all the technologies like java and .net also, then only our goal will must reach What you think your destiny…

    One feedback. please give a feedback script in all the pages like sun and ibm sites. That one most of the people give a suggestion and right things to you…. Thank you…

  2. Shahbaz Ahmed says:

    Dear Respected Vijay Kumar, Please it a request to u, can u explain me with complete example, what it Main Purpose, it a request, Thankx a Lot

  3. Ravi says:

    Hi Vijay,

    Gave a nice article with simple understanding.

    I need a small suggestion for the same.

    Let’s say I have a column called ‘country_name’, ‘country_code’ and ‘no_of_calls’ in both the queries.

    If the ‘no_of_calls’ from query1 is ’10′ for the ‘country_name’ USA and ‘country_code’ 1 and
    ’20′ for the ‘country_name’ India and ‘country_code’ 91 and
    ‘no_of_calls’ from query2 is ’15′ for the ‘country_name’ USA and ‘country_code’ 1.

    Now i want the o/p to be ‘country_name’ USA ‘country_code’ 1 and ‘no_of_call’ 25 and
    ‘country_name’ India ‘country_code’ 91 and ‘no_of_call’ 20.

    What I mean is, compare the two query results, if it matches with one another combine them, else just display them in the order they have.

    Hope u understand my question. Thanks in advance.

  4. Moillglenna says:

    buy best for promotion code with confident

Leave a Reply