How can i access in two different databases at the same time, i mean can i implement a search from database1 and save for example the id or another information to a database2?
If you have a user account that can access both databases, just prefix the database name:
SELECT database1.table1, database2.table2.....
as far as I know, you can even do JOINs, although I don't know about possible performance implications.
If you do not have a mySQL account that can access both databases, it's not possible.
Yes. PHP has a few MySQL extensions. However, if you're just getting started, look at the MySQL functions for this. Specifically mysql_connect() and mysql_select_db() could be used to create separate connections to different databases or switch between them on the same connection.
Related
So far we have a server with 2 databases and a mysql user that accesses any of them. For example
`select * from magento.maintable, erp.maintable`
now our erp is very slow and we want to separate our database on another server, but we have hundreds (almost a thousand) sql queries that have access in the same query to the two databases, for example
`insert into magento.table
select * from erp.maintable`
or
select * from erp.maintable inner join magento.table...
and more and more
How can I make everything work the same without changing these queries? but with the databases on different servers
To access the databases I have created a class for each database and through an object I make the queries, insertions, updates and deletions, like this
` public function exec($query, $result_array = true)
{
$this->data->connect();
$result = $this->data->query($query, $result_array);
$this->data->disconnect();
return $result;
}`
all help is welcome, the point is to find an optimal way to do this and not have to manually change 1000 sql queries made by another programmer
To access more than one database server in one query, you either have to use FEDERATED database engine or use replication to replicate the ERP-data from another server to the original one.
The use of FEDERATED engine is likely to cause additional performance problems and the replication requires some work to set up.
If the sole reason for the new server is the performance in ERP, you might want to see why the ERP is slow and try to solve that (optimize, move both databases to a new server, etc). When you have both databases on the same server, the query optimizer is able to combine and make efficient use of indexes.
I'm building a setup thing and it would be neat if I could check for the existence of a db on a given host by name (assuming the user and password provided have access to it).
I can't use information_schema because I might not have access rights to it.
I can't try selecting anything from a table because I don't necessarily know the names of the tables in the database.
Are there any other ways to do it?
Try using show databases. It'll list all databases you have access to.
Or if you want to be more specific: show databases like "dbname".
Also, you always have access to information_schema, at least to those records that are related to the databases you have access to.
Using PHP5, I'm needing to access two different databases (on the same server) but with different login credentials. Then get data from one database and update the other database for a record matching a unique field value. Here's what I have so far...
$link2->query("UPDATE db2.table2
INNER JOIN db1.table1
ON db1.table1.email = db2.table3.email
SET db2.table2.field2 = db1.table1.field1");
Where I am totally lost is how to handle the fact that both databases have different connections. Which connection to I query? How do I incorporate the connection of the other database?
To use a single query to hit two different databases, two things must be true:
Those databases must be on the same MySQL server.
The username you use must have appropriate access to both databases, and all tables involved.
If these two conditions don't hold you need to retrieve the information to your program with a connection to one database, and then update the tables in the other database with its connection. That can sometimes be hard.
I believe your best bet in this case is to persuade your database administrator to add privileges to the account you use to modify db2 so it also has access to read db1. (Of course that won't help if they're not on the sane server.)
I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.
I'm wondering how slow it's going to be switching between 2 databases on every call of every page of a site. The site has many different databases for different clients, along with a "global" database that is used for some general settings. I'm wondering if there would be much time added for the execution of each script if it has to connect to the database, select a DB, do a query or 2, switch to another DB and then complete the page generation. I could also have the data repeated in each DB, I just need to mantain it (will only change when upgrading).
So, in the end, how fast is mysql_select_db()?
Edit: Yes, I could connect to each DB separately, but as this is often the slowest part of any PHP script, I'd like to avoid this, especially since it's on every page. (It's slow because PHP has to do some kind of address resolution (be it an IP or host name) and then MySQL has to check the login parameters both times.)
Assuming that both databases are on the same machine, you don't need to do the mysql_select_db. You can just specify the database in the queries. For example;
SELECT * FROM db1.table1;
You could also open two connections and use the DB object that is returned from the connect call and use those two objects to select the databases and pass into all of the calls. The database connection is an optional parameter on all of the mysql db calls, just check the docs.
You're asking two quite different questions.
Connecting to multiple database instances
Switching default database schemas.
MySQL is known to have quite fast connection setup time; making two mysql_connect() calls to different servers is barely more expensive than one.
The call mysql_select_db() is exactly the same as the USE statement and simply changes the default database schema for unqualified table references.
Be careful with your use of the term 'database' around MySQL: it has two different meanings.