Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Andrew Kotze
Andrew Kotze
994 Points

How do I get a file off a PHP server with GET

I am putting some information onto a PHP Server using a POST command from FormCraft. I would like this information to populate a SQL DB on another Server.

1 Answer

You can configure where you want to persist your data when you set up your MySQL connection.

<?php
$database = 'database'; // Nane of the database on the other server
$mysql_host = '8.8.8.8'; // This should be the IP address of the other server

$destination = "mysql:dbname={$database};host={$mysql_host}";
$user = "databaseusername"; // Username to access MySQL on your other server
$password = "supersecretpassword"; // Password to access MySQL on your other server

try {
    $dbh = new PDO($destination, $user, $password);
}
catch (PDOException $e) {
   echo "'Connection failed: {$e->getMessage()}";
}
Andrew Kotze
Andrew Kotze
994 Points

Hi Corey. Thanks for your answer. The other server is not MySQL but MSSQL. I have since decided to send the .csv files to the SQL Server via FTP and then open/manipulate the data on the SQL Server so that I can write die contents to SQL. Do you have a better idea? Perhaps opening the files in PHP and then send the contents to SQL on the other Server?

I'm fairly certain PDO will allow you to connect to a MSSQL database as well as it will a MySQL database. It's implemented through PHP's PDO_DBLIB also available through PDO_ODBC. I would still as a matter of preference handle the form processing on the server the form is on and persist the results to the other server.

Try with my earlier solution but change the destination variable, and let me know how it goes.

<?php
$database = 'database'; // Nane of the database on the other server
$mssql_host = '8.8.8.8'; // This should be the IP address of the other server

$destination = "mssql:dbname={$database};host={$mssql_host}";
$user = "databaseusername"; // Username to access MySQL on your other server
$password = "supersecretpassword"; // Password to access MySQL on your other server

try {
    $dbh = new PDO($destination, $user, $password);
}
catch (PDOException $e) {
   echo "'Connection failed: {$e->getMessage()}";
}