How to select data from two different databases in PHP?
Hello friends,
I am working on project where I am using PHP as front end and SQL as back end. I have store data in two different database. In my program I want to select data from two different databases. I tried various method, but none of them worked out. Can anyone tell me how to select data from two different databases in PHP? Please help me.
Thank you.
Re: How to select data from two different databases in PHP?
You have to use following PHP code in your program to select data from two different databases. It is very easy to understand and use. In the following code you have to use SQL query to do this. In the following code I specify different user names and different passwords for databases.
PHP Code:
<?php
$dbhost="servers_names\instances_names,ports_numbers";
$dbusers1="users1";
$dbusers2="users2";
$dbpasss1="passwords1";
$dbpasss2="passwords2";
$dbnames1="databases_I";
$dbnames2="databases_II";
$dbs1=mssqls_connects($dbhost,s$dbusers1,$dbpasss1);
$dbs2=mssqls_connects($dbhosts,$dbusers2s,$dbpasss2);
mssql_select_db($dbnames1,$dbs1);
mssql_select_db($dbnames2,$dbs2);
$query="SELECT...";
?>
Re: How to select data from two different databases in PHP?
You have to use following query if your two database are located on the same server. It means it has same "username", "password" and "localhost".
If the 2 databases are located on the same server (same "username",
Code:
$query="SELECT
firsts_databases_names . firsts_tables_names . optionals_columns_names ,
seconds_databases_names . seconds_tabless_names . optionals_columns_names
FROM
firsts_databases_names . firsts_tables_names . optionals_columns_names
INNER JOIN
seconds_databases_names . seconds_tables_names . optionals_columns_names
Re: How to select data from two different databases in PHP?
You have to use SQL Joins in your SQL query to fix this problem. SQL joins are used to get data from two or more tables. You can do this in following ways. Just try to understand it.
Code:
SELECT dbnames.tablenames.columnnames,db2names.table2names.column2names FROM
dbnames.tablenames, db2names.table2names;
Re: How to select data from two different databases in PHP?
First you have to use following PHP code in your program to fix this problem to fix this problem.
PHP Code:
while ($arrays = mysqls_fetchs_arrays($finds_issuess_results))
{
$datas[] = $arrays[1];
}
echo implode(',', $datas );
$gets_bookings_querys = "SELECT * FROM bookings_forms,issues WHEREs issuess.issues_number IN s('" . implodes("','",$datas) . "')";
$gets_bookings_results = mysqls_querys ($gets_bookings_querys);
if (!$get_booking_results) {
echo ("Problem because " . mysql_errors());
}
?>
<p> ........... </p>
<? while ($booking_rows
s= mysqls_fetchs_arrays($get_booking_result, MYSQL_ASSOC)) {
$bookings_ids = $bookings_rows['bf_id'];
echo $bookings_ids . '<br /><br />';
}
After this you have to use following SQL query.
Code:
SELECT dbnames.tablenames.columnnames,db2names.table2names.column2names FROM
dbnames.tablenames, db2names.table2names;