Connect to two different databases fails

Posted on 2004-11-18
Last Modified: 2013-12-12
I have a problem connecting to two mssql databases in one php script.
- I made two different include file to connect to the two db's
- in the php script I include both files
- my first select statement look as follows:

$msquery1 = "SELECT Dateiname FROM database1.dbo.table";
$msquery1 .= "WHERE AuftragNr = '$jobNr'";
$msresult1 = mssql_db_query($msquery1,$link1);
while ($mszeile1=mssql_fetch_array($msresult1))
echo "<td>$mszeile1[Dateiname]</td>";
mssql_close ($link1);

- the second select statement in the same file:

$msquery = "SELECT AdrName FROM database2.dbo.table";
$msquery .= "WHERE AuftragNr = '$AuftragNr'";
$msresult = mssql_db_query($msquery,$link);
while ($mszeile=mssql_fetch_array($msresult))
echo "<td>$mszeile[AdrName]</td>";
mssql_close ($link);

I am totally stuck - when I take one select statement out and run the other one separately I get the field contents I ask for. I have no idea where I go the wrong way.

Thanks very much in advance.
Question by:MarionKi
    LVL 40

    Accepted Solution

    I would recommend using

    mssql_select_db($sDBName, $rConnection);

    prior to the mssql_query() calls.

    Try this first.

    LVL 40

    Expert Comment

    Did you cut and paste the code?

    There is no PHP method mssql_db_query.

    This is the list from the manual ...

    mssql_bind --  Adds a parameter to a stored procedure or a remote stored procedure
    mssql_close -- Close MS SQL Server connection
    mssql_connect -- Open MS SQL server connection
    mssql_data_seek -- Moves internal row pointer
    mssql_execute --  Executes a stored procedure on a MS SQL server database
    mssql_fetch_array --  Fetch a result row as an associative array, a numeric array, or both
    mssql_fetch_assoc --  Returns an associative array of the current row in the result set specified by result_id
    mssql_fetch_batch --  Returns the next batch of records
    mssql_fetch_field -- Get field information
    mssql_fetch_object -- Fetch row as object
    mssql_fetch_row -- Get row as enumerated array
    mssql_field_length -- Get the length of a field
    mssql_field_name -- Get the name of a field
    mssql_field_seek -- Seeks to the specified field offset
    mssql_field_type -- Gets the type of a field
    mssql_free_result -- Free result memory
    mssql_free_statement -- Free statement memory
    mssql_get_last_message --  Returns the last message from the server
    mssql_guid_string --  Converts a 16 byte binary GUID to a string
    mssql_init --  Initializes a stored procedure or a remote stored procedure
    mssql_min_error_severity -- Sets the lower error severity
    mssql_min_message_severity -- Sets the lower message severity
    mssql_next_result -- Move the internal result pointer to the next result
    mssql_num_fields -- Gets the number of fields in result
    mssql_num_rows -- Gets the number of rows in result
    mssql_pconnect -- Open persistent MS SQL connection
    mssql_query -- Send MS SQL query
    mssql_result -- Get result data
    mssql_rows_affected --  Returns the number of records affected by the query
    mssql_select_db -- Select MS SQL database


    Author Comment

    The wrong php method was copied from an earlier trial by mistake - but I took up your first suggestion and established the connection with each database directly before the select statement skipping the include files - and this at least now gives me a connection with both databases and gets me going again.
    Thanks very much.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now