Making PHP return SQL result to a different page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Walker

    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:[color=blue][color=green][color=darkred]
    >>>>>>>>>>>>[/color][/color][/color]
    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[color=blue][color=green][color=darkred]
    >>>>>>>>>>>>[/color][/color][/color]
    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




  • hex kid

    #2
    Re: Making PHP return SQL result to a different page

    David Walker wrote:[color=blue]
    >Hi[/color]
    [...][color=blue]
    >SQLinterp.ph p:[color=green][color=darkred]
    >>>>>>>>>>>>>[/color][/color]
    >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[color=green][color=darkred]
    >>>>>>>>>>>>>[/color][/color]
    >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
    ><<<<<<<<<<<<
    >
    >[/color]

    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:

    Comment

    • David Walker

      #3
      Re: Making PHP return SQL result to a different page

      > Have SQLinterp.php return the data as a XML/HTML text only file,[color=blue]
      > then parse it in mypage.php or the C++ application[/color]

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

      David


      Comment

      • James Sleeman

        #4
        Re: Making PHP return SQL result to a different page

        hex kid wrote:
        [color=blue]
        > 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
        > // ...
        > ?>[/color]

        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($da ta);

        // 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!

        Comment

        • Pedro

          #5
          Re: Making PHP return SQL result to a different page

          James Sleeman wrote:
          [...][color=blue]
          >or easier...[/color]
          [edited][color=blue]
          > use serialize() and unserialize()[/color]

          wow! very much better :)

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

          Comment

          Working...