how to use Multiple Mysql Databases with a SINGLE PDO connection? - php

I have a webpage that has to use multiple databases at a time. In localhost, it works perfectly fine but while hosting in online free hosting sites, I get error if I use more than one PDO connection and only the first database connection works. Is there any possible way to use multiple databases using a single PDO connection?
Please HELP!

When connecting to a host, you don't have to specify a database:
$dbh = new PDO('mysql:host=127.0.0.1','USER','PASS');
Just make sure to include the database name when running your queries as such:
select * from db1.table1
select * from db2.table2

Related

i dont wan to keep dbname before each table in mysql

I have installed my php website with database in IIS, windows 2012, The problem is it is accepting if i mention db name before table name for each query in my website. example below
select userid,profile_id,email,first_name,last_name,password,login_flag,usertype
from reqsbook.user
where email='$email'
and password='$password'
and usertype='$usertype'
actually i used to run this website in linux web server but as per my client requirement i have installed it in iis server.
please let me know is there any possibility for remove db name before table in all queries in my project or can i keep db name at once place for universal
Three ways to do this, assuming your database name is reqsbook.
Give the SQL command USE reqsbook; immediately after connecting from php to your dbms, before you give any SELECT or other queries.
if you're connecting with mysqli_, use something like this:
$mysqli = new mysqli('hostname', 'username', 'REDACTED_PASSWORD', 'reqsbook');
If you're connecting with PDO, try something like this
$db = new PDO('mysql:host=hosthame;dbname=reqsbook', 'username', 'REDACTED_PASSWORD');
If you're connecting with mysql_, well, stop doing that unless you want your customer's application pwned by cybercriminals.

Update Access Database from PHP script?

I'm relatively new to PHP and databases. Currently I'm working with an existing Access database and am able to read and display the data just fine. However, I'm wanting to update a user record through an email verification link. I have the server send an email with a link of www.domain.com/verify.php?userID=#
I am able to read this GET variable just fine, but I'm completely lost as to how I update the record. Everything I search for is for updating MySQL databases whereas I'm using odbc.
Does anyone know how I could set this up?
something like this:
odbc_exec($conn, "UPDATE table SET field=value"); // $conn is your connection identifier
You need to use a ODBC database client library in PHP to connect to a ODBC datasource. For example you can use PDO with the driver ODBC and DB2 Functions (PDO_ODBC).

php mssql multiple connection, by mistake connect1 records updating in connection 2 even connection2 closed properly

Application is PHP + MSSQL.
Wrongly update in multiple connections.
I have two connection.
opening connection1.
doing some process.
opening connection2. (Note: to open connection2 we need connection1 database.)
doing some updates in connection 2.
closing connection2.
update and insert process in connection1. but system wrongly updating into connection2 database instead of connection1.
How should handle the multiple connections.
You're probably not specifying the connection handler in your query calls. The mssql (like mysql) libraries defaults to using the LAST opened connection as the default.
so, in pseudo_code:
$con1 = mssql_connect("blah blah blah");
$result = mssql_query("SELECT blah blah blah");
$con2 = mssql_connect("blah blah blah"); <-- last opened, new default
$result2 = mssql_query("SELECT blah blah", $con1);
^^^^^---force using original connection
Found my own solution I think from a forum post.
The comment is below. My understanding of it is: If both databases have the same host and username mssql connect just returns the previous connection using those parameters. Dumb but thats whats happening.
Multiple calls to mysql_connect with the same parameters will only result in one resource/connection to be created. Once the first is created, subsequent calls to mysql_connect will return the same connection.
Once you call mysql_select_db, both connections will point to the same database. In my development, I got around this by using the IP for the servername in one mysql_connect and localhost for the second call. This created two seperate connections, but limits the number of seperate databases you could work with on the same server.
In one script another way to work this out would be to include the db name in the select statements or switch between databases using mysql_select_db just before the select.
I needed two distinct connections, since I dont' want to call mysql_select_db in my other classes.

MySQL Optimizations using CodeIgniter Active Records with Multiple Databases

I am using multiple databases using CodeIgniter Active Records class. I had to disable persistent connection with MySQL, because CodeIgniter Active Records class can't make persistent connections using multiple databases.
I am looking at the load of my database, it seems like it's using most of it's queries calling "change database" and I am not sure if that's a good sign.
How can I optimize this without having to call "change database" all the time?
It's not as user friendly as most of the Active Record commands, but you can call the SQL directly like this:
$query = $this->db->query("SELECT * FROM database_a.some_table");
$query2 = $this->db->query("SELECT * FROM database_b.another_table");
Are you using queries that reference both databases? If not, it's not too difficult to load a new DB instance for the second database. I.e. you'd still use $this->db for the first, but you could have $this->db2 for the second. I honestly have no idea if that would trigger the "change database" command you're talking about, but it would be MUCH more sustainable code, at least. CI could keep its connections to each database open for the duration of the script (not a persistent connection), and it seems your problem would be fixed.
I've never needed multiple mysql databases in a single app, so this is entirely a guess based on what I've seen with, say, one mysql db and another being an sqlite db.

Two db connect files

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.

Categories