Accessing my other database from my website 1 to my website 2 - php

I have website 1 currently uploaded in the web and i have also develop a website 2 running on the localhost for now.. I want to access or get some value from the website 1 database to my website 2..is this possible using php query or javascripting? if not, what approach i need to take? thanks for the help

Yes you can, You have to just pass the parameters of the server details like this example.
<?php
//Connect To Database
$hostname='ukld.db.5510597.hostedresource.com';
$username='myusername';
$password='mypassword';
$dbname='testdb';
//your rest of code
?>

To allow connections from an external IP-address, you will need to do the following as well:
Grant access to a new database
If you want to add a new database called foo for user bar and remote IP 202.54.10.20 then you >need to type the following commands at mysql> prompt:
mysql> CREATE DATABASE foo;
mysql> GRANT ALL ON foo.* TO bar#'202.54.10.20' IDENTIFIED BY 'PASSWORD';
More information

Yes, It's simple.
<?php
$hostname = "remote_host_name";
$database = "remote_database_name";
$username = "database_username";
$password = "database_password";
$con = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $con);
?>
Use this $con as the second parameter while running query.
e.g. mysql_query($query, $con);
Make sure that you have granted the access of server 1 in server 2 mysql database.

Related

Is the correct way to connect my website to my server?

I'm a total beginner when it comes to PHP, I have a fair grasp of the syntax but I'm not sure about the safest way to utilise it to connect to my server. I apologise that this is a sort of generic question rather than a code problem, since my code technically works.
I have a .php site doc with a basic comment submission form. The only way I can think of to connect to the server is to allow a "dummy" user with select only privelege to call a stored function to accept the comment.
If my dummy account is called siteuser then am I going round this the right way? This is the section of the PHP that I'm using to connect. I believe this code is only visible server side so nobody can ever see it and use the password or username to connect some other way? Or is there a sort of default string I can use in my php without creating the dummy user, seeing as the php and server is all hosted via the same provider?
$sqlserv = "localhost";
$sqlname = "siteuser";
$sqlpass = "mypassword";
$sqldbdb = "comments_table";
$conn = new mysqli($sqlserv, $sqlname, $sqlpass, $sqldbdb);
What i do is this to connect to my DB
db.php:
<?php
// Load configuration as an array. Use the actual location of your configuration file
$config = parse_ini_file('/somepath/config.ini');
//Mysqli Connection
$conn = new mysqli($config['host'], $config['user'], $config['pass'], $config['dbname']);
if($conn->connect_errno > 0){
die('Unable to connect to database [' . $conn->connect_error . ']');
//Set encoding
mysqli_set_charset($conn, "utf8") or die;
}
?>
and in config.ini:
[database]
user = johndoe
pass = someweirdpassword
dbname = the_name
host = localhost
both files have 700 permissions, so only user (and no one else can access it)
also the config.ini file is placed somewhere outside the public_html directory, i'm not totally sure if that helps or not but i do it that way.

One website - multiple database

We have multiple masters that are synced into a slave. We have decided to create a database for each master (let say MDB0001; MDB0002; MDB0003, etc...). This will allow to not corrupt the entire database if one replication fails or has corrupted data... The slave is used to show information to the people that are on the web (the master is only available in the local network)
The purpose is: we want to have a website (in php) on the server (slave) that shows the content for each database depending who is logged in. So if the user MDB0001 is connected, we have to read the data from the database MDB0001.
How can this be done? Is it a good way to do that? Or, do I have to duplicate the website for each database?
I hope I'm clear in my explanation. Thanks
assuming you get a variable from the login you could put a key->value array together on your db.php page;
$userDBs = array('login1'=>'db1','login2'=>'db2');
$dbName = $userDBs[$loggedinID]; // if login1 logs in, db1 would be result.
$db = new PDO('mysql:host=localhost;dbname='.$dbName, 'someUser', 'somePass');
or have a seperate db for the associations:
$sel = "Select dbName from databases where userId='".$loggedInID."'";
$stmt = $db->query($sel);
while($r = $stmt->fetch()){
$dbName = $r['dbName'];
}
$db = new PDO('mysql:host=localhost;dbname='.$dbName, 'someUser', 'somePass');

Does a PHP file that attempts to connect to a database have to exist on the same server as the DB?

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?
<?php
$servername = "xy.xy.xyz.xyz";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
No it doesn't. The DB server has to be configured to allow remote connections though.
You can check this for further details for MySQL.
No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.
Run the following command through MySQL
SELECT *
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';
Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'#'localhost' and nothing that looks like 'username'#'%', then your user is being limited to only localhost (same machine) access.
If you want to grant your user non-local access, you can use the following:
GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'#'%'
IDENTIFIED BY 'mypassword'
In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

php connecting to two databases on diff servers and using insert select

hi I need some help what I am trying to do is using insert select statement like this where databases are on diff servers.
insert into db1.copy2 (c1,c2) select c1,c2 from db2.copy2;
db1 is on amazon web service relation database and db2 is on local host(wamp) how can i make a PHP script that can accommodate two databases like that and that will allow me to execute a query like that. this script will execute on local host
thanks..
There are many pieces of software for syncronising or copying data between to database servers, maybe you should use one of those. Otherwise you could just do it in 2 steps
in pseudo code
$sql = "select c1,c2 from db2.copy2";
$Results = pdofetchall($sql);
foreach ($Results as $row){
$c1 = $row['c1'];
$c2 = $row['c2'];
$InsertSQL = "insert into db1.copy2 VALUES($c1,$c2) ";
executesql($InsertSQL);
}
Note this is pseudo code and defintitely cannot be copied and pasted, but give you the gist of what you need to do.
You have 3 options:
If you can connect the 2 servers via a vpn network or ssh connection so that the 2 servers can directly see each other, then you can use federated table engine:
The FEDERATED storage engine lets you access data from a remote MySQL
database without using replication or cluster technology. Querying a
local FEDERATED table automatically pulls the data from the remote
(federated) tables. No data is stored on the local tables.
This solution would enable the syntax you used in the question.
Again, you have to enable direct connection between the 2 servers and you set up replication between the 2 servers, essentialky making sure that the data available on the localhost is copied to the amazon server. The query would run on the amazon server and would query the copied data.
You combine data within php from the 2 data sources either by directly connecting to both instances or by setting up a web-based api on local server that can be used to transfer data from the local server to amazon.
you can do this easily if your 2 database on the same server, just make a connection to one database and pass your query easily:
$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);
}
$sql = "insert into db1.copy2 (c1,c2) select c1,c2 from db2.copy2;";
this will work for you

how i can Import data from Database ( My Sql) to another website

I need to show data from another website in my website,
i had control panel for tow site but i need it dynamic way to connect them
website 1 website 2
***** *****
* A * <<<< =Data== * B *
***** *****
so i write
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.");
#mysql_select_db("qatarlab_test",$link)or die("Couldn't select database");
$factorRes = #mysql_query("SELECT count(id) FROM `factor` ");
$factorRow = #mysql_fetch_array($factorRes);
echo $factorRow[0];
but nothing happen
Firstly, you must enable remote database connection in the original site so that the new site can connect to it. you can do this in the control panel->Database(Remote Database Connection) of the original site.
You could specify the IP or domain of the new site to that it allows only the new site to connect or you could just add wildcard (%) to allow any external connection to the database.
when this is done, run ur query again.
hope it helps
Hi Please try code given below,
$link = #mysql_connect("Ip Addres to another website","username for mysql 2","passowrd 2 ")or die("Couldn't make connection.") or die(mysql_error());
mysql_select_db("qatarlab_test",$link)or die("Couldn't select database", $link) or die(mysql_error());
$factorRes = #mysql_query("SELECT count(id) FROM `factor` " , $link) or die(mysql_error());
$factorRow = #mysql_fetch_array($factorRes);
echo "<pre>";
print_r($factorRow);
echo "</pre>";
Here $link will be for specific database connection and die(mysql_error()) for if any error in or query or database connection.
thanks
In your first line: mysql_connect you are passing the IP Address and Username for connecting the DB.
Make sure that the user has the privilege to connect to the DB remotely (i.e. via a machine other than the Localhost). In general, when a user is added in the DB its default access is for the localhost. Check out this link: http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
You basically need to add user using: CREATE USER 'admin'#'IP Address of Website 1'; All the CPanel's have this provision in their interface as well.

Categories