MySQL::GetColumnName PHP Method

GetColumnName() public method

This function returns the name for a specified column number. If the index does not exists or no records exist, it returns FALSE
public GetColumnName ( string $columnID, string $table = "" ) : integer
$columnID string Column position (0 is the first column)
$table string (Optional) If a table name is not specified, the last returned records are used.
return integer Field Length
    public function GetColumnName($columnID, $table = "")
    {
        $this->ResetError();
        $result = false;
        if (empty($table)) {
            if ($this->RowCount() > 0) {
                $field = mysqli_fetch_field_direct($this->last_result, $columnID);
                if (!$field) {
                    $result = false;
                    $this->SetError();
                } else {
                    $result = $field->name;
                }
            } else {
                $result = false;
            }
        } else {
            $records = mysqli_query($this->mysql_link, "SELECT * FROM " . $table . " LIMIT 1");
            if (!$records) {
                $this->SetError();
                $result = false;
            } else {
                if (mysqli_field_count($this->mysql_link) > 0) {
                    $field = mysqli_fetch_field_direct($records, $columnID);
                    if (!$field) {
                        $result = false;
                        $this->SetError();
                    } else {
                        $result = $field->name;
                    }
                } else {
                    $result = false;
                }
            }
        }
        return $result;
    }

Usage Example

Example #1
0
 function dumpTableData($table)
 {
     $conn = new MySQL(true, DBNAME, DBHOST, DBUSER, DBPASS, "", true);
     $strTables = "SELECT * FROM `{$table}`";
     $conn->QueryArray($strTables);
     $cntTables = $conn->RowCount();
     if ($cntTables) {
         $num_fields = $conn->GetColumnCount($table);
         $this->output .= "\n\n";
         $this->output .= "-- \n";
         $this->output .= "-- Dumping data for table `{$table}` \n";
         $this->output .= "-- \n\n";
         $fields = '';
         $field_type = array();
         $i = 0;
         while ($i < $num_fields) {
             if (!$fields) {
                 $fields = "`" . $conn->GetColumnName($i, $table) . "`";
             } else {
                 $fields .= ", " . "`" . $conn->GetColumnName($i, $table) . "`";
             }
             //                array_push($field_type, $conn->GetColumnDataType($i, $table));
             $i++;
         }
         $conn->MoveFirst();
         while (!$conn->EndOfSeek()) {
             $rsTable = $conn->RowArray();
             $this->output .= "INSERT INTO `{$table}` ({$fields}) VALUES (";
             for ($i = 0; $i < $num_fields; $i++) {
                 if (is_null($rsTable[$i])) {
                     $this->output .= "null";
                 } else {
                     switch ($field_type[$i]) {
                         case 'int':
                             $this->output .= $rsTable[$i];
                             break;
                         case 'string':
                         case 'blob':
                         default:
                             $this->output .= "'" . addslashes($rsTable[$i]) . "'";
                             break;
                     }
                 }
                 if ($i < $num_fields - 1) {
                     $this->output .= ", ";
                 }
             }
             $this->output .= ");\n";
         }
     }
     $this->output .= "\n";
 }