Fetching data from multiple databases with MySQLi - php

i'am using MySQLi to connect and fetch data from my MySQL server with
$link = new mysqli("localhost", "root", "","database_1");
I have a file that used for connection and data collection (dboperations.php) from above database
Now , i need to connect another database (e.g. database_2) and fetch data in the same php file.
Conditions:
Are databases on the same server? YES
Am i authorized to connect with same username and pass? YES
Is there any way to do that? Thanks.

Use mysqli::select_db to switch databases on the same server:
$link = new mysqli('localhost', 'root', '', 'database1');
then
$link->select_db('database2');

SELECT * FROM database_2.table ...

Related

Can I connect more than one database into phpmyadmin using php?

Can we connect more than one database at a time in phpMyAdmin using PHP? And yes then how?
Here I use a variable database_name which contain database name and for connecting take a variable $database_selection and connect a single database which name is criminal_circum. If I want to connect more than one database instead of single database so,what should I do?
$database_name = "criminal_circum";
$db_handle = mysqli_connect($db_server_name, $db_user_name, $db_password);
$database_selection = mysqli_select_db($db_handle, $database_name);

How to include two database connections?

I have included 1 database connection in my PHP file. The file that connects to the database is called connect.php. This works successfully with no errors. When I try to include 2 database connections in my PHP file, this is when I encounter errors. It seems as though, I can only connect to one database at a time because only 1 of the databases are connected. Is there a way to include 2 database connections in 1 file?
This is what I have right now:
<?php
require "connect.php";
require "informational-connect.php";
?>
This is what's included in connect.php:
<?php
$db= new mysqli("localhost", "XXX", "XXX", "ARTICLES");
if($db->connect_errno)
{
die("Error");
}
?>
This is what's included in informational-connect.php:
<?php
$db = new mysqli("localhost", "XXX", "XXX", "INFORMATION-DATA");
if ($db->connect_errno)
{
echo die("Error");
}
?>
I am using MySQLi.
In order to have different connections you have to give the connection variables different names
Besides, you need only one database to deal with, and therefore only single connection
The database selection is associated with the connection resource. So you can select it once for each connection.
If you don't call mysql_select_db at all,then all your queries will need to specify explicit database prefixes before all the tables, e.g. select * from db1.table ....
mysqli is provide a functionality for connect multiple database at a time.
But How one single variable in a file can hold two different
connections ?
You should use two different variables for each database connection.
$Db1 = new mysqli($hostname,$username,$password,$db_name1); // connection 1
$Db2 = new mysqli($hostname,$username,$password,$db_name2); // connection 2

how to Post data from wordpress to other server(mysql) database

I am working on a word press plugin where i have some data to be saved in another mysql server database.Simply the data have to be saved in another MySql server's database table.Hope i am Clear.How can i approach to this progrmmatically.Any suggestions are highly appreciated.Thanks in advance
You will have to read up on PHP's methods for creating a database connection. Once you establish an additional database connection (using IP, port, and login credentials for the second database), you'll be able to interface with it through SQL. You'll be able to assign the new database connection to a handle so you can easily control which database you're sending the SQL queries to and avoid communicating with the wrong one.
Please make a new database connection with new instance and save data with other database.
for eg.
$dbh1 = mysql_connect($hostname, $username, $password);
$dbh2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);
mysql_query('select * from tablename', $dbh1);
mysql_query('select * from tablename', $dbh2);

Connecting to a remote mysql server using a function

I'm using wordpress and I have a remote mysql server for some other data not stored on wordpress db.
Now I want to connect to that remote mysql server in a form of a php function but i don't know how or if it is possible. Basically this function serves as to check if $orderid exist on the remote mysql server.
function check_orderid($orderid) {
// Connect to database
// Check if $orderid exist if yes return true.
}
The remote server does accept remote connections.
Also, Is it okay and will not affect wordpress db connection?
Connecting to a remote server is not different from connecting to a local server, besides the host address. Just replace the 'localhost' with a resolvable dns name, or the ip address from your remote server, and you should be fine.
To make multiple connection you have to use links.
they are created after successful connect:
$link1 = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$link2 = mysqli_connect('localhost', 'my_user2', 'my_password2', 'my_db2');
you can use them like:
mysqli_query($link1, "your first query as string");
mysqli_query($link2, "your second query as string");
By official OO version you should use:
$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = new mysqli('externalIP', 'my_user2', 'my_password2', 'my_db2');
$mysqli1->query("some query");
$mysqli1->query("some other query");
so, in this case you can easily do this:
$externaldbcon = new mysqli('ExtrnalDBIP', 'my_user', 'my_password', 'my_db');
$externaldbcon->query("your query for external DB");

PHP connecting to DB : OOP vs Procedural 1 dont work 2 works

I am scratching my empty head with this issue:
When I have my logging data as procedural, everything works fine. I require the file once and can proceed with my select queries. If the connection to the DB file is however written as OOP. I get the error that No database selected
====
<?php
// SETTING VALUES AS CONSTANTS
DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'tra');
// CONNECTING JUST TO THE SERVER
$dbc = #mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$dbc) // IF IT CAN'T CONNECT, ISSUE A MESSAGE
{
die('Could not connect: ' . mysql_error());
}
// CONNECTING NOW TO THE DB
mysql_select_db("tra", $dbc);
?>
So that up there works fine. I have got that in a separate file, (out of the root path)
But if I try to do it OOP, then, the rest of the queries that are to work once I have the access to the DB won't work. I sometimes get the query written as output, I mean as if not executing, just the plain text:
$mysqli = new mysqli("localhost", "root", "");
$mysqli->select_db("tra");
I have tried tons of varieties with the OOP version, first instantiating separately such as
$mysqli = new mysli();
$mysqli->connect("127.0.0.1", "root", "", "tra");
or also
$mysqli = new mysqli("localhost", "root", "");
$mysqli-> select_db("tra");
but none of the tries with OOP will work. I mean, I have the book on my lap, so I am writing it ad litteram. The issue must be elsewhere.
The PHP version is 5.3.8 about the latest one.
The sql query that follows after the connection and selection of the DB (which works if the connection file is procedural, as I say) is:
$sql ="SELECT FName
FROM work_assignment, developer
WHERE developer.country = '".$country."'
AND work_assignment.from_language = '".$from."'
AND work_assignment.into_language = '".$into."'
AND work_assignment.developer_id = developer.developer_id
";
Any ideas as to why?
Thanks a lot
From your comment I surmise you're mixing the mysql and mysqli extensions. They are two separate extensions with nothing in common, except that they both connect to MySQL. If you're connecting to the database using a mysqli object, use that object to run your queries. mysql_query will have no active connection, because you did not establish one using a mysql function.

Categories