Making PHP return SQL result to a different page

This is a discussion on Making PHP return SQL result to a different page within the PHP Language forums, part of the PHP Programming Forums category; Hi I have a Microsoft SQL database I can use (also mySQL, so if you know how to do this ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-27-2003
David Walker
 
Posts: n/a
Default Making PHP return SQL result to a different page

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




Reply With Quote
  #2 (permalink)  
Old 06-27-2003
hex kid
 
Posts: n/a
Default Re: Making PHP return SQL result to a different page

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:
Reply With Quote
  #3 (permalink)  
Old 06-27-2003
David Walker
 
Posts: n/a
Default Re: Making PHP return SQL result to a different page

> 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


Reply With Quote
  #4 (permalink)  
Old 06-28-2003
James Sleeman
 
Posts: n/a
Default Re: Making PHP return SQL result to a different page

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!
Reply With Quote
  #5 (permalink)  
Old 06-30-2003
Pedro
 
Posts: n/a
Default Re: Making PHP return SQL result to a different page

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:
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:22 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0