PHP - Multiple MySQL Connections [duplicate] - php

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

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";
?>

How to fix: Error- No database selected

SERVER RESPONSE
Error: INSERT INTO epiz_19848473_Liste1 (Rowcount, Level1) VALUES (0, 'any Value')
No database selected
PHP CODE
UPDATE
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "Liste1";<--UPDATED
// Create connection
$conn = mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Liste1 <--UPDATED (Rowcount, Level1)
VALUES (0, 'any Value')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
This should work, right?
NEW STATUS
Connection failed: Access denied for user 'epiz_19848473'#'192.168.0.%' to database 'Liste1'
The code is perfectly all right , the problem is with mysql configuration .
Please login to mysql as **root user ** and do the following,
GRANT ALL PRIVILEGES ON . TO 'epiz_19848473'#'192.168.0.%' (use ip of php server)
FLUSH PRIVILIGES;
The above allows the user to connect to mysql table from the ip specified.
you did not defined the database name in the connection
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "DATABASE_NAME_HERE";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
else you can define define the database name in the connection
$conn = new mysqli($servername, $username, $password, "DATABASE_NAME_HERE");
Just define the dbname,like this:
<?php
$servername = "sql308.epizy.com";
$username = "epiz_19848473";
$password = "huihuibuh";
$dbname = "test"; //You should create this db in mysql
....
You Can Use This:
CREATE TABLE [database_name].[table_name] (
[your_table_things]
);

I want to connect with my: online MySQL database, but I get this error. Can someone help me?

I want to make connection to my online database, but then I get this error:
Warning: mysqli :: mysqli (): ( HY000 / 2002 ): A connection attempt failed because the " Related party Incorrectly If the answer after a certain time , of the costs Connection failed because " the Confederate host has not responded . C: \ wamp \ www \ array Add in db \ 222.php on line 8
This is my code:
?php
$servername = "db.tapdeleest.nl"; $username = "***"; $password = "***"; $dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "nice";
}
?>
Thanks for helping!
Using new mysqli() you don't add the database name in your connection.
You use mysqli_select_db($conn, $dbname)
<?php
$servername = "db.tapdeleest.nl"; $username = "***"; $password = "***"; $dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "nice";
mysqli_select_db($conn, $dbname);
}
?>
Just advice: Give pdo a try, I feel like pdo is easier.
http://www.w3schools.com/php/php_mysql_connect.asp

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

How to connect two databases in 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);

Categories