How to connect two databases in php? - php

i want to select two databases in php but when i code it gives error how i select two databases here is my code which i already tried:
<?php
#session_start ();
$dbhost = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "domain102,main102";
$mysqli = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname);
if ($mysqli->connect_errno)
{
echo ("Failed to connect to MySQL: " . $mysqli->connect_error);
}
$GLOBALS ['mysqli'] = $mysqli;
?>
here is error
Warning: mysqli::mysqli(): (HY000/1049): Unknown database 'domain102,main102'

Try like
$dbname1 = "domain102";
$dbname2 = "main102";
$mysqli1 = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname1);
if ($mysqli1->connect_errno)
{
echo ("Failed to connect to MySQL: " . $mysqli1->connect_error);
}
$mysqli2 = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname2);
if ($mysqli2->connect_errno)
{
echo ("Failed to connect to MySQL: " . $mysqli2->connect_error);
}
You cont connect two databases with same instance or even we can say in single instance.

"Selecting a database" simply means you set a default for which database in a server you're querying. If you didn't do that, you'd have to prefix all tables in your queries:
SELECT * FROM database1.table1
I guess that is what you're really trying to do here. You cannot "select two databases" at once because that doesn't make sense by the definition of what "selecting a database" means, but you can query other databases you haven't selected by simply prefixing the tables with the database name in your queries.
You can also switch to a different database on the server you're connected to at any time with mysqli::select_db.

You can connect to one database server and create queries as #deceze suggested.
Moreover you can use query with USE keyword to switch between databases
USE db1;
SELECT COUNT(*) FROM mytable; # selects from db1.mytable
USE db2;
SELECT COUNT(*) FROM mytable; # selects from db2.mytable
http://dev.mysql.com/doc/refman/5.0/en/use.html

Use mysqli::select_db to switch databases on the same server:
$link = new mysqli($dbhost, $dbusername, $dbpassword, $dbname);
then
$link->select_db($dbname2);

If you want to use two databases then
Just use following code:
$dbhost = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "domain102";
$mysqli = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname);
$dbname = "main102";
$mysqli1 = new mysqli ($dbhost, $dbusername, $dbpassword, $dbname);

Related

MYSQL Database connection for php

I am using my website database with php mysql_connect but my hosting provider is saying I need to use mysqli_connect.
When I edit connection file and update change method its not working with mysqli.
Currently using this code:
MSQL Method:
$con=mysql_connect("localhost","username","password");
mysql_select_db("databasename");
and for mysqli I am using this code but fail
MYSQLI Method:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Please help. Why is mysqli not working? Is my method wrong?
Try this:
Example (MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
` $dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

PHP - Multiple MySQL Connections [duplicate]

This question already has answers here:
Connect to Multiple Databases using MySQLi
(3 answers)
Closed 6 years ago.
Ok so this is my code for the connection:
$servername = "host";
$username = "user";
$password = "pass";
$db1 = "db1";
$db2 = "db2";
// Create connection
$conn = new mysqli($servername, $username, $password, $db1); // $db1 is here as the default, is this ok?
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then I run a function with this connection and it works for the $db1:
$sql = "SELECT etc etc;
$result = $conn->query($sql);
etc etc
The problem is when I try to change the db to $db2, I use this:
$conn->select_db($db2);
And now it only returns the value of the 2nd Database ($db2), and the one from $db1 doesn't show up on the page anymore, it doesn't show any value.
Thanks for Reading.
You have use two different connections while connecting different databases:
Database One Connection:
$conn = new mysqli($servername, $username, $password, $db1);
$sql = "SELECT etc etc;
$result = $conn->query($sql);
Database Two Connection:
$conn1 = new mysqli($servername, $username, $password, $db2);
$sql = "SELECT etc etc;
$result = $conn1->query($sql);
The Problem is that you have already assigned the conn to DB1 and hence if you run by selecting the DB2 it will be displaying error or no results will be produced. Hence while selecting multi databases you can use different connection variables.
the one from $db1 doesn't show up on the page anymore, it doesn't show any value.
what about making it $conn->select_db($db1); back?
Create two config files
db1.php
$servername = "host1";
$username = "user1";
$password = "pass1";
$db = "db1";
db2.php
$servername = "host2";
$username = "user2";
$password = "pass2";
$db = "db2";
//connection to db1
include("db1.php");
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//db1 codes
--------------------
//connection to db2
include("db2.php");
$conn = new mysqli($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//db2 codes

Need help connecting PHP to MySQL database

I am building a website for a friend and he would like some statistics on the website. The statistics are on a dedicated MySQL server, and the website is on another. I have been trying to keep linking the database to the website but it keeps failing. I am using PHP to attempt this.
The code I have been trying to use is here:
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The database name is the issue. Any ideas about this?
mysqli_connect takes 4 arguments, not 3 as you are trying. The fourth one is database name. Documentation is here.
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Edit: The argument is optional, but if it's is a shared hosting, your credentials are probably working for the one specific database, not the whole server. So specifying the database could help you with this issue.
mysqli_connect needs database name to execute
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
$dbname ="x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
use
$dbname='yourdbname';
mysqli_select_db($conn,$dbname);
to select the database

PHP script cannot access database in sql

I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli

php syntax database connect

I'm trying to learn this stuff. Please be gentle.
Something is wrong here:
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysql_select_db($dbname . $dbconnected) or die ($dberror1);
where is my mistake? I want $dbconnected to show...
I can just as easily use
echo "hello";
it shows that I connect but I'm trying to get familiar with using multiple variables.
would this be better?
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname))
echo $dbconnected;
else die($dberror1);
Right now you are trying to connect to a database called something_dbnameyou are connected. The . concatenates variables into one string.
To fix your immediate problem, try this:
First, define $dbhost - I don't see it in your code.
Then change the last line to this:
$select_db = mysql_select_db($dbname) or die ($dberror1);
Then, just echo $dbconnected;
If you are not connected, the page will have called die, and will never reach the line that echos $dbconnected. If you are connected, the program will proceed to this next line and echo your success message.
Or you can do it more explicitly like this:
if ($select_db = mysql_select_db($dbname))
echo $dbconnected;
else die($dberror1);
To fix the bigger problem, DON'T use mysql_*. Read this for more information.
mysqli or pdo are far better options, and you can accomplish the same task easier, for instance, connecting to a db with mysqli is just:
$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
Or you can do it procedural style, which is closer to your current code. The following snippet is from the php manual, on the page I linked in the comment below.
$link = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
I'd strongly recommend using PDO. The connection string is similar and can be done using:
// I do not see $dbhost defined in your code. Make sure you have it defined first
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $dbconnected; // will print out the connection success message
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
To answer your question about not using mysql_* functions, you can check out this

Categories