2

I'm working on a small project at work. The intention is to receive data from my MS SQL Express server 2008 to a webpage. This is all working fine. I use the ODBC connect extension to do this. I have set a callender, so you can pick date's between you want to receive. But like i said, this is all working fine.

My problem: I want to get data from diffrent SQL servers. 3 in total. My sql statement is on every sql server the same. The only thing that need to change is the connection string and the database. Here you will find my PHP Code. I will use the $_POST in php ro receive the action from my form.

In HTML you can see that i use a select option. Whenever i choose for 'Grimbergen' the connection string must change to $connection_string = $grim; And when I choose for 'Gent' the connection string must change

$connection_string = $topo;

if (isset($_POST['knop'])) {
    $user = 'someuser'; 
    $pass = 'somepassword';
    $grim = 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_GRIM;DATABASE=UniCla';
    $topo = 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_TOPO;DATABASE=UniCla';
    $connection_string = ;  //don't know how i get this with $_POST!!!
    echo $connection_string;
    $connection = odbc_connect( $connection_string, $user, $pass ); 

    if (odbc_error()) {
        echo odbc_errormsg($connection);
    }
    $connection = odbc_connect( $connection_string, $user, $pass ); 

    // you must set the connection first
    if (odbc_error()) {
        echo odbc_errormsg($connection);
    }

    $bdate= $_POST['bdate'];
    $edate= $_POST['edate'];
    $grim_s = 'SRGR';
    $topo_s = 'SRTO';
    $stmt = "SELECT String1, String3, String13, String15, String4, String8, InOut, Weight1, Date1, Time1, Weight2, Date2, Time2, Nett FROM dbo.Move WHERE Date1 BETWEEN '$bdate' AND '$edate' AND String13='$grim_s' ORDER BY Date1 desc;";
    $result = odbc_exec($connection, $stmt);

And the HTML:

<form enctype="multipart/form-data" method="post" name="formulier" id="formulier" action="">
    <div>
        <h4>Site</h4>
        <p>
            <select name="place" id="place" class="form-control" >
                <option value="grim">Grimbergen</option>
                <option value="gent">Gent</option>
            </select>
        </p>
    </div>
    <div>
        <h4>Begin Date</h4>
        <input name="bdate" id="picker1" class="form-control" type="text">
    </div>
    <div>
        <h4>End Date</h4>
        <input name="edate" id="picker2" class="form-control" type="text">
    </div>
    <br>
    <div>
        <button type="submit" value="login" class="btn btn-info btn-fill pull-right" name="knop" id="knop">Submit</button>
    </div>
</form>
4
  • You meant get the data from different database, bur same query ? Commented Jun 15, 2016 at 9:24
  • Yes. The query is the same. Commented Jun 15, 2016 at 9:30
  • okay Is that grim and gent are database or tell your database name Commented Jun 15, 2016 at 9:32
  • When you select Grimbergen in HTML the connection string must change to 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_GRIM;DATABASE=UniCla'. When you select Gent in HTML the connection string must be 'DRIVER={SQL Server};SERVER=10.10.10.10\UNIWIN_TOPO;DATABASE=UniCla' Commented Jun 15, 2016 at 9:34

1 Answer 1

1

Fine Use select value as your DB Name like this

        <select name="place" id="place" class="form-control" >
            <option value="GRIM">Grimbergen</option>
            <option value="TOPO">Gent</option>
        </select>

And when you post use one connection string like this

$connection_string ='DRIVER={SQL server};SERVER=10.10.10.10\UNIWIN_'.$_POST['place'].';DATABASE=UniCla';
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, i didn't mension this. The IP address is also different. For Grimbergen it is 10.10.10.10, and for Gent 10.10.11.10. Is this also possible?
Thats fine use simple IF condition. if($_POST['place'] == 'GRIM'){ $ip="10.10.10.10";}else{$ip="10.10.11.10";} $connection_string ='DRIVER={SQL server};SERVER='.$ip.'\UNIWIN_'.$_POST['place'].';DATABASE=UniCla';
Thank you. This has helped me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.