S. Walker
S. Walker

Reputation: 53

PHP MySQL Two queries

I would like to Know how can I in PHP code, execute two mysql query (query_1 and query_2) but display only the results of query_2 for each result of query_1 like that :

query_1==> `SELECT tbl_name FROM table_ref `

query_2 ==> `SELECT id,name FROM (result of the first query)`

query_1 return the names of tables : table1, table2, table3, and with the query_2 I have to do this :

SELECT id,name FROM table1 and SELECT id,name FROM table2 ... as in a loop

Thanks in advance for your advice !

Upvotes: 1

Views: 232

Answers (2)

Angelin Nadar
Angelin Nadar

Reputation: 9300

On php side, you can deal with both the aray results as foll:

$array1 = array('tbl_name1' => '' ,'tbl_name2' => '');

$array2 = array('tbl_name1' => array('id' => 'id1'),
                'tbl_name2' => array('id' => 'id2'));

echo '<pre>';
print_r(array_intersect_key($array2, $array1));
echo '</pre>';

o/p:

Array
(
    [tbl_name1] => Array
        (
            [id] => id1
        )

    [tbl_name2] => Array
        (
            [id] => id2
        )

)

Upvotes: 0

S.Visser
S.Visser

Reputation: 4725

SELECT id, name FROM sometable WHERE name in (SELECT tbl_name FROM table_ref)

In this example there need to be a connection between 2 tables

Upvotes: 1

Related Questions