By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,564 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,564 IT Pros & Developers. It's quick & easy.

Making PHP return SQL result to a different page

P: n/a
Hi
I have a Microsoft SQL database I can use (also mySQL, so if you know how to
do this in mySQL that is just as useful). The database can only be accessed
from webpages hosted on the same server as the SQL database because of
firewalling. The database admin doesn't mind me accessing it like this if I
can do it, and they would allow me if the firewall could be changed (which
it can't / won't be). However, I need to get direct access to the database
from a different webserver - there is no way around the firewall, and I
can't put the pages on the same webserver for other reasons which aren't
important.
Anyway, what I thought I could try would be a PHP page on the same webserver
as the SQL database. I could then call this page from other pages elsewhere
on the internet, send the query in a POST or GET string (probably post
because of the greater size), the PHP script then queries the SQL server,
and then somehow returns the result to the original calling page. I can't
really work out how to get the return values back to where they need to be,
although I thought putting them in a standard array rather than as database
objects would be easier.
The main problem i'm having is how to return the values to the other page
without calling away from that original page, and how to get the output into
an array or something I can use.
Basically, I would have a script on the server called say SQLinterp.php and
the calling page of mypage.php. These would work something like as follows:

SQLinterp.php:
>>>>>>>> Read in the query from POST data
Verify and then execute the query on the SQL server
Get the return values, put them into some sort of array
Somehow (magic?) get this array back for use in the other page
<<<<<<<<<<<<

mypage.php>>>>>>>>

Draw page headers.......
Query database for some values
Go through and use the database values for various constructions of the page
and tables
More queries
More use of the data
End of page
<<<<<<<<<<<<

Now, what I though I may be able to do is include this as an 'include'
file,so it just executes the query inline and the array it creates, or even
just the normal result, is accessible normally. There is a complication
though, in that I also need this (or a slightly modified version) to
function when called from a C++ application, so simply including it would
not help for this case.
Any ideas greatly appreciated.
Thanks

David


Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David Walker wrote:
Hi [...]SQLinterp.php:
>>>>>>>>>Read in the query from POST data
Verify and then execute the query on the SQL server
Get the return values, put them into some sort of array
Somehow (magic?) get this array back for use in the other page
<<<<<<<<<<<<

mypage.php>>>>>>>>>

Draw page headers.......
Query database for some values
Go through and use the database values for various constructions of the page
and tables
More queries
More use of the data
End of page
<<<<<<<<<<<<


Have SQLinterp.php return the data as a XML/HTML text only file,
then parse it in mypage.php or the C++ application
SQLinterp.php
<?php
// read the database
// and output something like this
echo "
<data>
<row><id>1</id><name>David</name></row>
<row><id>4</id><name>hexkid</name></row>
</data>
";
?>
mypage.php
<?php
// ...
$data = implode('',
file('http://database.server/SQLinterp.php?' .
'q="select id, name from users"')
);
// and now parse $data
// ...
?>

HTH

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #2

P: n/a
> Have SQLinterp.php return the data as a XML/HTML text only file,
then parse it in mypage.php or the C++ application


Ahhh - thanks for that, never thought to do it that way.

David
Jul 16 '05 #3

P: n/a
hex kid wrote:
SQLinterp.php
<?php
// read the database
// and output something like this
echo "
<data>
<row><id>1</id><name>David</name></row>
<row><id>4</id><name>hexkid</name></row>
</data>
";
?>
mypage.php
<?php
// ...
$data = implode('',
file('http://database.server/SQLinterp.php?' .
'q="select id, name from users"')
);
// and now parse $data
// ...
?>


or easier...
SQLinterp.php
<?php
$data = array();
// Fill $data with rows from your query
echo serialize($data);
?>

mypage.php
<?php
$data = trim(implode('',
file('http://database.server/SQLinterp.php?' .
'q="select id, name from users"')
));
$data = unserialize($data);

// Work with $data, no parsing needed :-)
?>

--
James Sleeman
Gogo:Code http://www.gogo.co.nz/
Email domain : gogo.co.nz see user in from header!
Jul 16 '05 #4

P: n/a
James Sleeman wrote:
[...]
or easier... [edited] use serialize() and unserialize()


wow! very much better :)

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.