Can I connect more than one database into phpmyadmin using php? - 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);

Related

How can I create database in SQL inside PHP file

I already create a database in MySQL using
CREATE DATABASE register
And I set
$db= "CREATE DATABASE register";
$host="localhost";
$dbusername="root";
$dbpassword="";
$dbname= "register";
//create database connection
$conn = new mysqli($host,$dbusername,$dbpassword,$dbname);
but some error that becomes error
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'register' in C:\xampp\htdocs\web\html\reg.php on line 19
How can I fix that
You don't have to specify a database:
$conn = new mysqli($host, $dbusername, $dbpassword);
You can then issue your query with $conn->real_query($db) (which is OK because you're just issuing a statement without any user data) and use $conn->select_db to make the database the active db after creating it.
Your assignment in the beginning ($db = ...) doesn't do anything else than assign the value to a variable - the query isn't ran before you actually send it to MySQL.

how to use pg_connect to connect to 2 different postgresql databases on the same computer

I am trying to use 2 instances of postgresql database on the same computer. any idea how should i use pg_connect
I was able to connect to one instance of postgresql using pg_connect successfully. However I have created a second instance for a new web application. How to specify parameter to connect to both databases on the same php code using pg_connect.
It is literally the first example given on php.net reference page for pg_connect
$dbconn = pg_connect("dbname=mary");
//connect to a database named "mary"
$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"
$dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password
Just change the connection string and declare multiple connection objects and you should be fine

Database connection phpmyadmin

I'm working for an e-commerce that has the db on phpmyadmin. In another website I'd like to connect to that database. I have password, username and db name, so I'm using this "connection string":
<?php
$nomehost = "localhost";
$nomeuser = "user";
$password = "pass";
// connection
$conn=mysql_connect($nomehost,$nomeuser,$password);
if (!$conn) exit ("Error connection<br>");
// db name
if (!mysql_select_db("db_name",$conn)) exit("Error db name<br>");
?>
The result is "Error db name". What can I do? Have I to set some oprion in the phpmyadmin?
First of all:
this error is caused by the fact that you are selecting the wrong database in your MySql server. Is your db called db_name???
EDIT: based on the comments you are making: is the server that hosts the php page the same as the mysql server?
Then:
phpmyadmin is just a tool to connect and handle MySql databases and is not a database server itself.
Last but most important:
you are using a deprecated library (mysql) in php to connect to a MySql server. Please consider moving to mysqli or better to PDO

Fetching data from multiple databases with MySQLi

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 ...

MySQL table doesn't exist when it does exist

I have this query:
mysql_select_db('scanner');
$query = "SELECT * FROM scanner.proxy ORDER BY RAND() LIMIT 20";
$result = mysql_query($query) or die(mysql_error());
it tells me:'scanner.proxy ' doesnt exist.. even though I do have it the table with the database. It is weird, cause my other queries work..but this one doesnt.
UPADTE:
Event when I put this:
$user='root';
$user='root'
$password='<removed_password>';
$dbname = 'scanner';
$host = 'localhost';
$link = mysql_connect($host, $user, $password) or die (mysql_error());
mysql_select_db($dbname, $link) or die (mysql_error());
it gives me this...
Unknown database 'scanner'
But I can see the scanner database in the phpmyadmin
Even when I type in phpmyadmin the sql statement
SHOW TABLES FROM 'scanner'
it says it cant find scanner
We solved this problem which occurred when connecting to multiple remote MySQL databases within the same script by adding a re-connect method before every mysql_query statement. The re-connect method invoked both mysql_connect and mysql_select_db functions. We had previously tried setting the new link parameter to true in the connect statement(s) but We still got database errors and all kinds of funny behavior.
make sure the user you use to connect has enough rights to query that table. Perhaps that table was created by another user.
EDIT: Perhaps you need to grant access to scanner from a remote location. Running this sholud help on that.
GRANT ALL ON scanner.* TO your_user#'IP_ofServer_where_PHP_runs' IDENTIFIED BY 'PASSWORD';
You have set the $dbname = 'aws_backlinks', but you are trying to connect to a different database within your query. Change your connection string to include this db instead or just connect to the server and set your database at the time of the query.
$db_host = 'localhost:Port';
Specify port for localhost.
I was facing the same problem and specifying the port solved the problem.
You're missing the argument that specifies the connection (result of mysql_connect()) in your mysql_select_db() call:
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'whatever';
$db_name = 'scanner';
$link = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $link);

Categories