Reputation: 2969
I have a dedicated Linux Server with my multiplayer game (C++/Sockets) on it and recently I acquired a MySQL server "service" for my web pages (I also have a little multiplayer game done in PHP) so I don't need to think about backups etc.
The MySQL service works wonderfully well for my web pages but I can't connect from the server to the database as they are not on the same internal networks. The only connections that can be done must come from web pages hosted on the providers servers.
I have thought of sending requests from the server (the C++ game server) to a PHP web page that connects to the database and sends back the answer. This will work for any simples request (like how many HP has player X) but when it comes to iterative requests (ie. "SELECT id FROM player") where there are a lot of answers it becomes more complicated.
Is there a standard way to circumvent this? Is there already proof read PHP / C++ code somewhere out there? Any other way (ie some sort of fake port forwarding)?
Or should I bite the sour apple and start fiddling with Linux automated backups?
In this case, speed is not an issue, data integrity and reliability is.
Upvotes: 1
Views: 3480
Reputation: 2502
Your question basically suggests proxy for MySql requests with C++ client and PHP as server. While it is completely possible and there can be some good solutions(PHP can work with raw sockets, so creating a proxy shouldn't be so much of a problem) you have one more limitaion - you don't have root access so you can't create any sockets. That leaves us only with HTTP protocol.
Executing remote queries by HTTP protocol is possible: for example http://www.phpclasses.org/package/4000-PHP-Execute-remote-MySQL-queries-across-the-Web.html is an example PHP client and PHP server. But there will always be severe limitations: each separate request will be a separate MySql connection, so some MySql features will be hard to do preoperly: temporary tables etc.
The more complex queries you want to execute the more complex your application will get: prepared statements, MySql escaping, getting last insert id etc. all have different transfer formats, so they all have to be written on both your C++ client and PHP server.
Error chance will increase too: you will get additional HTTP errors etc. One of the reasons MySql in PHP is so popular is because it is reliable: thousands of developers/applications use it each day and it can be considered stable. But you can still find some bug reports about MySql driver for PHP, so imagine how many bugs will be in some rarely used code? And more, using rare programs is almost always bad idea - if you get some obscure error one day there will be nowhere to look for solution.
On the other hand there are a lot of programs/scripts/advices how to do automatical MySql backups and most likely on your Web(PHP) server they are done with one of well known ways that you can do yourself.
Upvotes: 2
Reputation: 6272
I'm not sure if I understand the question, but I'll give it a shot. You would generate an array of your SQL commands client side using whatever language you would like...make sure to handle escaping. Take that array, encode it using Serialize, JSON, etc. Pass the plain text string through post to the PHP api. on the php side:
mysql_connect($server, $username, $pass);
@mysql_select_db($db) or die("Cannot select DB.");
$unencoded = unserialize( $_POST['input'] ) //Match for w/e encoding you used
$cnt=0;
foreach( $unencoded as $query){
mysql_query($query) or die(mysql_error());
$cnt++
}
return "$cnt Queries";
This is going to be dangerous though as the above is expecting fully escaped strings. I would definitely also include some sort of hash validation to avoid exploits. You could also use SOAP requests, but I highly doubt the extension is enabled. Maybe an alternative to the simple array structure would be something a little more complex, which would allow you to escape the user generated portions...something like this maybe?
$queries['SELECT'][$cnt] = array( 'cols'=>"*", 'from'=>'table', 'where'=>'condition' );
then loop through on the php side, using mysql_real_escape_string on the values of the array.
Upvotes: 1
Reputation: 737
If your concern is backups, you definitely should go for automated backups, it's very simple. Let's say you want to backup your mysql database every day at 12 am, use this cron job:
0 0 * * * mysqldump dbname -u username -ppassword > /path/to/store/backup
You can then just download this backup if you want to store it offsite.
Upvotes: 1