Esempio n. 1
0
function ColNames($tbl, $db_name)
{
    global $db, $current_col_count;
    global $one_gig_a, $one_gig_z, $two_gig_a, $two_gig_z;
    $col_names = "";
    $current_col_count = 0;
    $query = "sp_columns {$tbl}";
    $result = @sqlsrv_query($db, $query);
    if (FALSE == $result) {
        $err_msg = get_sql_error();
        print "\r\n";
        print "In database: " . $db_name . "\r\n";
        print "Can not query table: " . $tbl . "\r\n";
        print $err_msg . "\r\n";
        return -1;
    }
    do {
        while ($row = sqlsrv_fetch_array($result)) {
            $name = trim($row[3]);
            $dtype = trim($row[5]);
            $precision = intval(trim($row[6]));
            if ($precision >= $one_gig_a && $precision <= $one_gig_z) {
                $precision = "1 Gig";
            }
            if ($precision >= $two_gig_a && $precision <= $two_gig_z) {
                $precision = "2 Gigs";
            }
            if ($precision > $one_gig_a) {
                $precision = number_format($precision);
            }
            $length = trim($row[7]);
            if ($length >= $one_gig_a && $length <= $one_gig_z) {
                $length = "1 Gig";
            }
            if ($length >= $two_gig_a && $length <= $two_gig_z) {
                $length = "2 Gigs";
            }
            if ($length > $one_gig_a) {
                $length = number_format($length);
            }
            //$nullable = trim($row[10]);
            $column_def = trim($row[12]);
            $is_nullable = trim($row[17]);
            if (0 == strlen($is_nullable)) {
                $is_nullable = "UNKNOWN";
            }
            $current_col_count += 1;
            if (strlen($name) <= 7) {
                $tab = "\t\t\t\t\t";
            } elseif (strlen($name) <= 15) {
                $tab = "\t\t\t\t";
            } elseif (strlen($name) <= 23) {
                $tab = "\t\t\t";
            } elseif (strlen($name) <= 31) {
                $tab = "\t\t";
            } else {
                $tab = "\t";
            }
            $last_comma = strlen($column_def) ? ", def: " : "";
            $col_names = sprintf("%s\r\n%s%s%s, %s, %s [%s]%s %s", $col_names, $name, $tab, $dtype, $is_nullable, $precision, $length, $last_comma, $column_def);
        }
    } while (sqlsrv_next_result($result));
    sqlsrv_free_stmt($result);
    $col_names = substr($col_names, strlen("\r\n")) . "\r\n\r\n";
    return $col_names;
}
Esempio n. 2
0
function RowCount($tbl, $db_name)
{
    global $db;
    $query = "select count(*) from {$tbl}";
    $result = @sqlsrv_query($db, $query);
    if (FALSE == $result) {
        $err_msg = get_sql_error();
        print "\n";
        print "In database: " . $db_name . "\n";
        print "Can not query table: " . $tbl . "\n";
        print $err_msg . "\n";
        print "Will return -1 for the # of rows, but this will not effect overall row counts.\n\n";
        return -1;
    }
    do {
        while ($row = sqlsrv_fetch_array($result)) {
            $count = $row[0];
        }
    } while (sqlsrv_next_result($result));
    sqlsrv_free_stmt($result);
    return $count;
}