I have a table in a hosting I want to communicate with it remotely through my localhost, but I can't reference that I want the table from that database how do I do that?
*im using php *
<?php
// Create connection
$conn = mysqli_connect('localhost', 'root', '','contador');
$conn2 = mysqli_connect('hostingIP', 'userIp', 'userPass');
$tableName='counter';
$dbname2='dbHosting';
$result = mysqli_query($conn2,$dbname2);
$sql = "INSERT INTO tab2 (Data,price) VALUES ( now(),(SELECT COUNT(*) FROM $result.$tableName WHERE DATE(Data)= CURRENT_DATE() - INTERVAL 0 DAY))";
?>
Make an export script in localhost.
Trigger it regularly via cron jobs.
Include FTP credentials to upload it to your server.
Make an import script in your server.
Trigger it regularly via cron jobs.
Thank me later :)
Related
I need to know if it's possible to Create an Event in MySQL with its code statement using Load Data In file syntax.
Here's my current situation.
I can get the database (MS Access) using PHP from a Local Biometric Database.
I have updated the database in my Local Database Server which is MySQL now.
I can update my target Online Database Server via Exporting it first then Importing the database in the Online Database Server.
My Main Question is how I can manage this without any user interaction
such as MySQL Event Features?
I am trying to upload a data from my PC (Local server) into our Domain (Web Server)
Already done this guys, What i did is I've built a local application to my local server at our office then that application is doing a FTP service to upload the updated database from our Local Biometric Database into the online web server.
Once all this is done, I've created a php script which will load the .SQL file using a Cron Job. You all know cron job are time scheduled events for controlling scripts and doing other linux command.
I'll share my code of php file so everyone who encounter this can use this in the future.
date_default_timezone_set('Asia/Taipei');
$CurTime = date('Y-m-d H:i:s');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Failed to connect to server: ' . mysqli_error());
}
//Select database
$db = mysqli_select_db($link,DB_DATABASE);
if(!$db){
//Do auto download of database in the Server automatically
if(file_exists('Database/'.DB_DATABASE.'.sql')){
mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents("Database/".DB_DATABASE.".sql");
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
} else {
die("Unable to select database, Please call your system administrator. Thank you.");
}
}
function Reload_Database($DBLink){
if(file_exists($DBLink)){
//mysqli_query($link,"DROP DATABASE ".DB_DATABASE);
//mysqli_query($link,"CREATE DATABASE ".DB_DATABASE);
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db = mysqli_select_db($link,DB_DATABASE);
$readContent = file_get_contents($DBLink);
$res = mysqli_multi_query($link,$readContent) or die(mysqli_error($link));
mysqli_close($link);
return 'Database Reloaded';
} else {
die("Unable to reload database, Please call your system administrator. Thank you.");
}
}
echo Reload_Database('../employee/database/'.DB_DATABASE.'.sql');
This is basic script that will reload the database of the uploaded database given from my local application.
From here the cron job will do its part. Expect that my local application is also a service that will just do the uploading of the updated database of the Local Biometric Database.
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
im trying to write a query to insert data to my remote database table from a CRON file in my remote server. db connection happens through webservice and all that is working fine but the database does not get filled. following is the code that i have written, will anyone of you be able to find why it does not get write to the database table
$link = DbConnection::getInstance('TDC')->connectMysql()
$attributeKey = "12345";
$query_new = "INSERT INTO redtable (message) VALUES ('$attributeKey')";
$result = mysql_query($query_new, $link);
I am trying to send database table from one server to another database table in different server. Here is the PHP code I have tried so far, but to no avail:
<?php
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 1
mysql_select_db('db1_name',$dblink1); // select database 1
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
$table='output_table';#not sure if this is correct
$tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE first_table ",$dblink1)); // get structure from table on server 1
echo $tableinfo;
mysql_query(" $tableinfo[1] ",$dblink2); // use found structure to make table on server 2
$result = mysql_query("SELECT * FROM first_table ",$dblink1); // select all content
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
mysql_query("INSERT INTO output_table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')",$dblink2); // insert one row into new table
}
mysql_close($dblink1);
mysql_close($dblink2);
echo "complete";
?>
I have tried the output_table with and without the same column names/amount as first_table, but it does not seem to be working.
Again, any help would be appreciated.
The problem might be that $dblink2 is undefined.
$dblink2=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
Also consider using pdo methods instead because mysql- methods are now less favored to the PDO way of working with database objects.
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
o to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used
Ok first off this is not a backup job, and I know about using phpmyadmin and so on.
What I have is a Live server and a TEST server, People work on the live one, constantly modifying it. I want the database on the LIVE server to be copied to the TEST server every night.
Does anyone have a php script I could run either to copy down the whole thing (or at least specific tables)? I'm not that confident with using the command line so if there is a way of doing it that way I could do with some pretty specific instructions :P
I have dug around but everyone seems to be suggesting doing it manually or by using third party tools.
Any help much appreciated.
You could do something like this:
In your mysql host machine:
1- Create a .sh file
2- Inside of this sh, put:
- mysqldump -u myuser -p mypass mydatabasename > mydumpfile.sql
- scp mydumfile.sql user#remote_host:remote_dir
3- Add that sh to a cron Job, to a daily execute
(or something that meets your requeriments)
In the remote machine:
1- One sh script that look for a file(mysqldumpfile.sql) in the specific dir
2- This line : mysql -u remotemysqluser -p remotemysqlpass database < mydumpfile.sql
3- rm mydumpfile.sql
4- Add this sh on a daily cron 1 or two hours past the host cron.
Drop the test tables -
DROP TABLE testdata;
And then recreate them as copies of the live tables -
CREATE TABLE testdata LIKE livedata;
INSERT INTO testdata SELECT * FROM livedata;
This could be done through PHP like this -
<?php
$host = '127.0.0.1';
$dbname = 'database'
$user = 'user';
$pass = 'pass';
try {
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
$STH = $DBH->('DROP TABLE testdata');
$STH->execute();
$STH = $DBH->('CREATE TABLE testdata LIKE livedata');
$STH->execute();
$STH = $DBH->('INSERT INTO testdata SELECT * FROM livedata');
$STH->execute();
$DBH = null;
?>
You can add extra tables as required, but in my example it will make a table called testdata that mirrors the table called livedata.
Then set up a cron job to fire the script when required -
php /path/to/script.php