Forum Moderators: coopster

Message Too Old, No Replies

Reading database from another site

reading mysql database of another site

         

kenchix1

11:40 am on Jul 6, 2005 (gmt 0)

10+ Year Member



is it possible to read a database from another site without using javascript or vbscript?

site1 gives data to site2
site2 display data from site1

something that works like rss? (i don't know how to create this kind of script.)

My purpose is to integrate my two websites, the first website will supply the information needed then the second site will diplay the information on certain locations of the site.

I already learned how to read/display/manipulate mysql data if the database is located on the same host (server) but not on a separate host.

please help.

thanks in advance.

mcibor

12:42 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can for sure do that, because mysql_connect [php.net] has that option included. However I don't recommend it because of safety reasons, as well as I don't know how to configure mysql to allow that.

What you can do however is use the site1 to process the data and spit it out.

on site2:
<?php
include("www.site1.com/site2.php?search=bla");
?>

on site1 file site2.php:
<?php
$search = mysql_real_escape_string($_GET["search"]);
//connect to db, get the results
//print the results
echo "Results for search string $search are:"<br>
print_r $results;
?>

This is also not the best way, but will work.
Michal Cibor

brendan3eb

1:04 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



I believe if you have WHM, there is part of WHM that lets you set if other servers can connect to SQL on your server.

kenchix1

2:51 pm on Jul 6, 2005 (gmt 0)

10+ Year Member




This is also not the best way, but will work.
Michal Cibor

Thanks for the idea! i'll try that. :)

brendan:

I have whm, I use it for my multiple sites, i'll check. thanks! :)

kenchix1

8:24 am on Jul 8, 2005 (gmt 0)

10+ Year Member



I tested the code and used this line in my first module:

include 'http://site1.kom/sub1/doit-onsite1.php'

and use the module on site2. it worked.

Since I need to return the value to a variable, I put the codes in a function named getxdata() inside doit-onsite1.php without changing anything on the first module except for the variable that will receive the value. It said:

Call to undefined function: getxdata() in /home/site2/public_html/fetch-site1data.php on line 100

something might be wrong on how I include or call the function on site 2 module?
:( :( :(

mogenshoj

8:36 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



How about just creating a custom php file with the content you want on site1 and then read that file from site2 and print it. Wouldn't that work? Haven't tested it, but it makes sense in my head anyway :)

$fp = fopen("http://www.site1.com/my_feed.php", "r");
while (!feof($fp)) $content = fgets($fp, 4092);
fclose($fp);

print "$content";

Edit.
Of course remember to chmod the file on site1 so it can be read from site2.

mcibor

7:52 pm on Jul 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aaa, sorry. I didn't tell you, that including sth on another server is parsed there, not here. So no variables are passed as such (only get) and no functions are passed.

Hope this cleares things for you.
Mogenshoj, your way is the same as include, however include works through apaache on the server there, when fopen may be able to read clear php (not execute it). And you don't want to execute that php here, but there.

Best regards
Michal Cibor

PS. My explanation may get a bit confusing. if so, don't hesitate to write.

mogenshoj

10:14 am on Jul 10, 2005 (gmt 0)

10+ Year Member



As i remembered fopen would execute php on server1, and the function that wouldn't be included using include() would also be executed on server1, correcting the problem.

But maybe i remember it wrong, haven't used the filesystem in a while.

kenchix1

8:14 am on Jul 11, 2005 (gmt 0)

10+ Year Member



Michal Cibor:

thanks, I understand what you said. I tested the function on the same site, it work, but will not on another site.

Mogenshoj :

thanks also, the file was succesfully read on the other site. It was read, not executed.

Here's another idea (just idea, i don't know how to do this)

site1 with call a function to site2 that will create a temporary flat file for the 10 lines of data, then another function from site1 will read the flat file from site2 then load it in an array? (how to put lines in an array?)

comments please?

thanks.

mcibor

9:41 am on Jul 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To read file into an array use file() [php.net] function. It will allow that.

If you read plain db I recommend storing the data in csv format, which is easy to read and write (with implode() [php.net] and [url="httl://www.php.net/explode]explode[/url])

Hope this will help you
Best regards
Michal Cibor

TaelonSecurity

10:35 am on Jul 13, 2005 (gmt 0)

10+ Year Member



Use a hack!

If your remote script looks like...

------------------

<?php

print 'foo';

?>


------------------

Turn it into...

------------------

<?php 

if ($_SERVER['REMOTE_ADDR']!= $your_webserver_ip)
die();

print '<?php ';
?>

print 'foo';


------------------

Local PHP will then evaluate your code.