PDA

View Full Version : Need help with PHP and Databases across servers [renamed]



bandinopla
August 11th, 2005, 02:05 PM
It is posible to make something like... lets said I have some phps files in a server... and in another, others... And I whant to those phps files in the other server make a query to a database that have them... and then, pass all the info to my server... something like, asigning to a variable a function from another php in another server and that function return wathever it found...

ahmed
August 11th, 2005, 02:08 PM
Um, exactly what are you trying to do? Try looking into PHP's cURL library:

http://ca.php.net/curl

bandinopla
August 11th, 2005, 02:13 PM
I trying to do something that in a magical world I could do like this... (I said magical bicouse I don't know if it can be done)

$results=required("http://anotherServer/results.php?query=what")

So the results are worked in another server and pass it to me to my server.

ahmed
August 11th, 2005, 02:20 PM
I guess you could do something like.. echoing php tags and in between that, echo the stuff from the database such that you can include that file in your other server's php file

If my post does not make a lot of sense, please provide a sample of your code where you retrieve data from mySQL



[edit]

sorry, i'm stupid. PHP does not allow including a file that's from another server. I wasn't sure if it did, I tested it out right now and the method I suggested above is not possible

bandinopla
August 11th, 2005, 02:24 PM
:q: will U write me that in php or pseudocode....

ahmed
August 11th, 2005, 02:32 PM
Sorry, see edit above

bandinopla
August 11th, 2005, 02:36 PM
Yes,.. I try that to and nothing... there most be a way... but I tell You... Your idea is quite interesting.... I will be working on that in this hours...

ahmed
August 11th, 2005, 02:41 PM
Oh, actually.. It's possible using cURL (and maybe some string parsing) :)

What cURL let's you do is have the server act as a client. You have Server A and Server B. Server A sends out a request a request to Server B and captures the output (with your information it) using cURL functions. I'm at work right now, but I have the exact code required for this at home. I will post it later on today

bandinopla
August 11th, 2005, 02:50 PM
greate!

bandinopla
August 11th, 2005, 03:10 PM
PD: I'm also trying to do this:

mysql_connect('$IP:$PORT',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

but the "Unable to select database" is always there... I don't know why?

Mabe the IP is wrong... I don't know the IP very well...

Anyway... I think that the cURL functions will work...

ahmed
August 11th, 2005, 07:09 PM
Try this out



$query = "select * from mytable;";

$ch = curl_init("http://SERVER_B/page.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&query=" . $query );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
curl_close($ch);


and for your other file, the query will basically be $_POST['query'];


Your next step would be to echo your data on server b in a certain format. This will be stored in variable $data. After than you do whatever string manipulation required in order to get your information into an array :)

Hope this helps

bandinopla
August 12th, 2005, 07:54 AM
It look like my solution... I will try it... finaly... :thumb:

bandinopla
August 12th, 2005, 08:16 AM
I don't know what i'm doing wrong...

in this url: http://www.bandinopla.com.ar/phpA.php this is the content of that php:


<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="" content="text/html; charset=iso-8859-1">
<meta http-equiv="" content="text/html; charset=iso-8859-1">
</head>

<body>
<?

$query = "select * from mytable;";

$ch = curl_init("http://usuarios.lycos.es/bnpla/phpB.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "&query=" . $query );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

?>
</body>
</html>


And in http://usuarios.lycos.es/bnpla/phpB.php I have:



<?

echo "Se recibio " . $_POST['query'];

?>


For testing porpusess... U see something wrong... I, when write the first url, don't see the echo "Se recibio " . $_POST['query'];... isted i see a blank page...

:(

bandinopla
August 12th, 2005, 08:25 AM
it is possible that my server don't have instales the CURL paquet?

I im reading this link: http://ar.php.net/manual/en/ref.curl.php

Requirements

In order to use the CURL functions you need to install the CURL package. PHP requires that you use CURL 7.0.2-beta or higher. In PHP 4.2.3, you will need CURL version 7.9.0 or higher. From PHP 4.3.0, you will need a CURL version that's 7.9.8 or higher. PHP 5.0.0 requires a CURL version 7.10.5 or greater.

ahmed
August 12th, 2005, 11:02 AM
Yeah. run phpinfo() to see if you've got that module installed

bandinopla
August 12th, 2005, 11:18 AM
yeas... they don't seems to have it... :-(

ahmed
August 12th, 2005, 11:30 AM
I would contact the host and see what they can do for ya

bandinopla
August 12th, 2005, 12:52 PM
:thumb: thanks!