Table and Column Lists

  • I have a program, written in php, to give administrators the ability to dnamically run sql queries. Unfortunately, the table and column names for the test group were hard coded. I want to be able to dynamically generate this list so that I do not have to update the code every time I change the db.

    I have looked at the MS sites and through everywhere I could think of on the net, and I cannot find these commands. Is there some where in the db or a command I can use to get these lists?

    Thanks.

  • 
    
    <?php
    /* Get the connection */
    $cnn = mssql_pconnect(SQL_SERVER, SQL_USER, SQL_PASS);
    /* Select the DB */
    mssql_select_db(SQL_DB_NAME);
    /* Grab a resultset */
    $sql = [Your sql statement here];
    $oR = mssql_query($sql);
    foreach ($oField = mssql_fetch_field($oR)) {
    /* Now you have your field object...
    echo $oF->name . "\n";
    echo $oF->numeric . "\n";
    }
    ?>

    This will print the field names in your sql query...

    --

    Alternatively, you could query the SQL database system tables for other info:

    
    
    <?php
    /* Get the connection */
    $cnn = mssql_pconnect(SQL_SERVER, SQL_USER, SQL_PASS);
    /* Select the DB */
    mssql_select_db(SQL_DB_NAME);
    $table_name = 'MyTable';
    /* Get the columns from your table */
    $sql = "SELECT sc.name
    FROM syscolumns sc
    INNER JOIN sysobjects so
    ON so.id = sc.id
    WHERE so.type = 'u'
    AND so.name = '" . $table_name . "'
    ORDER BY sc.colid";
    /* Get the query */
    $oR = mssql_query($sql);
    /* Print the column names */
    $i = 0
    while ($oRow = mssql_fetch_row($oR)) {
    echo "Column" . $i . ": " . $oRow[0] . "\n";
    $i++;
    }

    HTH,

    Jay

    Edited by - jpipes on 08/12/2003 12:07:14 PM

  • Jay,

    The second is what I needed. Thanks for the help.

  • hi!

    take a look at http://www.sqlservercentral.com/scripts/contributions/246.asp

    best regards,

    chris

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply