mssql database control with php

Posted on 2011-05-13
Last Modified: 2012-05-11
I'm making a PHP script for controlling my mssql databases. But I ran into a couple of problems.

The first problem is that I want to make a combobox out of all the databases in my microsoft SQL server. With following code, but it doesn't seem to work.
SELECT name from sys.databases where owner_sid != 0x01

Open in new window

My next problem is, that I have to run follwing SQL command:
RESTORE FILELISTONLY FROM DISK = 'C:\Databases\Breda\backup\Backup.BAK'

Open in new window

But I need to get the first column of every row, and place that in 2 variables, but I don't know how to do that.

Can anyone provide me this code?
Question by:Silencer001
    LVL 1

    Expert Comment

    To answer your first problem, I believe you have to do this:

    "SELECT name from master.sysdatabases where owner_sid != 0x01 "

    I am not understanding your second question completly

    Author Comment

    And how do I put that code in a Combobox?

    My second question is that if I execute that SQL statement, the following result comes up
     Output sql commandAnd I need to put the 2 LogicalNames into a variable.

    So that row 1 column 1 (Breda) = $name1
    and row 2 column 3 (Breda_log) = $name2

    Because I need to use those 2 variables in the following code.

    Accepted Solution

    Found the solution, for the first problem it was:
    $query = mssql_query("SELECT name FROM sys.databases");
    		// Check if there were any records
    				if (!mssql_num_rows($query)) {
    					echo 'No records found';
    					} else {
    					echo "<select name='dbname'>";
    						for ($i = 0; $i < mssql_num_rows($query); ++$i) {
    						$lookup = mssql_result($query, $i, 'name');
    							if ($lookup != "master" && $lookup != "tempdb" && $lookup != "model" && $lookup != "msdb" && $lookup != "SBO-COMMON" && $lookup != "RSP")
    								echo "<option value='".$lookup."'>".$lookup."</option>";
    					echo "</select>";

    Open in new window

    and for the second problem it was:
    $query = mssql_query("restore filelistonly from disk = '".$locatie."'");
    	$lognaam1 = mssql_result($query, '0', 'LogicalName');
    	$lognaam2 = mssql_result($query, '1', 'LogicalName');

    Open in new window


    Author Closing Comment

    Code works for 100%

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now