Home > Archive > PHP Language > May 2004 > Reading from a remote database
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reading from a remote database
|
|
| Erik Kullberg 2004-05-26, 8:32 am |
| I use MySQL and Apache.
I need to read from and write to a data base on a remote server, from a php
script that runs on my local computer/server.
I guess I could make it in two steps - place a script on the remote server
and call that from a script on my local server. But how do I make it
directly from the local computer?
========================================
======
Erik Kullberg
Address: Enhagsvägen 2
S-589 43 Linköping
Sweden
Tel home: +46 (0)13 21 94 35
Tel mobile: +46 733 56 19 56
E-mail: erik.kullberg@telia.com
My place: N 58 deg, 23.115' - E 015 deg, 42.137'
| |
| Vasiliy Keretsman 2004-05-26, 11:34 am |
|
"Erik Kullberg" <erik.kullberg@telia.com> wrote in message
news:sC%sc.428$dx3.5541@newsb.telia.net...
> I use MySQL and Apache.
>
> I need to read from and write to a data base on a remote server, from a
php
> script that runs on my local computer/server.
> I guess I could make it in two steps - place a script on the remote server
> and call that from a script on my local server. But how do I make it
> directly from the local computer?
>
Study the parameters to mysql_connect() function in php-manual.
http://www.php.net/mysql_connect
| |
| Brian Ferrell 2004-05-26, 11:34 am |
| "Vasiliy Keretsman" <kman@ukr.net> wrote in message
news:c929pb$dvh$1@news.ukr.net...
>
> "Erik Kullberg" <erik.kullberg@telia.com> wrote in message
> news:sC%sc.428$dx3.5541@newsb.telia.net...
> php
server[color=darkred]
>
> Study the parameters to mysql_connect() function in php-manual.
> http://www.php.net/mysql_connect
>
>
yea you should be able to directly connect to the database from a remote
server. some hosts have restrictions to only allow access from certain
places for security reasons, but not a lot so you shouldn't run into that
problem.
-Brian
| |
|
| Disclaimer: untested.. numerous security holes.. i personally would NEVER do
something like this...
Local File:
<?
// data you want in remote table
$data_arr = array(
'column1' => 'value1',
'column2' => 'value2',
'column3' => 'value3'
);
foreach ($data_arr as $k => $v) {
$data_str .= $k . '=' . urlencode($v) . '&';
}
$data_str = substr($data_str, 0, -1);
$host = 'www.your-remote-server.com';
if (!$fp = @fsockopen($host, 80)) {
exit('Failed to connect to remote server.');
}
fputs($fp, "POST /remote_script.php HTTP/1.1\r\n");
fputs($fp, "Host: " . $host . "\r\n");
fputs($fp, "Content-type: application/x-www-form- urlencoded\r\n");
fputs($fp, "Content-length: " . strlen($data_str) . "\r\n");
fputs($fp, "User-Agent: Y Tu Madre Tambien\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data_str);
while (!feof($fp)) {
$res .= fread($fp, 10240);
}
fclose($fp);
echo $res;
?>
Remote File
<?
if ($_SERVER['HTTP_USER_AGENT'] != 'Y Tu Madre Tambien') exit();
// assuming mysql...
$db_conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db_name', $db_conn);
$sql = 'INSERT INTO `table_name` ';
foreach ($_POST as $k => $v) {
$columns .= "`" . $k . "`, ";
$values .= "'" . mysql_escape_string(stripslashes($v)) . "', ";
}
$sql = "INSERT INTO `" . $tbl . "` (" . substr($columns, 0, -2) . ") VALUES
(" . substr($values, 0, -2) . ")";
if (!$rs = @mysql_query($sql, $db_conn)) {
exit('Fatal Error: ' . mysql_error());
}
echo 'Inserted ' . mysql_affected_rows() . ' rows.';
?>
- Rook.
"Erik Kullberg" <erik.kullberg@telia.com> wrote in message
news:sC%sc.428$dx3.5541@newsb.telia.net...
> I use MySQL and Apache.
>
> I need to read from and write to a data base on a remote server, from a
php
> script that runs on my local computer/server.
> I guess I could make it in two steps - place a script on the remote server
> and call that from a script on my local server. But how do I make it
> directly from the local computer?
>
> ========================================
======
> Erik Kullberg
> Address: Enhagsvägen 2
> S-589 43 Linköping
> Sweden
> Tel home: +46 (0)13 21 94 35
> Tel mobile: +46 733 56 19 56
> E-mail: erik.kullberg@telia.com
> My place: N 58 deg, 23.115' - E 015 deg, 42.137'
>
>
|
|
|
|
|