MySQL::GetColumnNames PHP Méthode

GetColumnNames() public méthode

Returns the field names in a table or query in an array
public GetColumnNames ( string $table = "" ) : array
$table string (Optional) If a table name is not specified, the last returned records are used
Résultat array An array that contains the column names
    public function GetColumnNames($table = "")
    {
        $this->ResetError();
        if (empty($table)) {
            $columnCount = mysqli_field_count($this->mysql_link);
            if (!$columnCount) {
                $this->SetError();
                $columns = false;
            } else {
                for ($column = 0; $column < $columnCount; $column++) {
                    $field = mysqli_fetch_field_direct($this->last_result, $column);
                    $columns[] = $field->name;
                }
            }
        } else {
            $result = mysqli_query($this->mysql_link, "SHOW COLUMNS FROM " . $table);
            if (!$result) {
                $this->SetError();
                $columns = false;
            } else {
                while ($array_data = mysqli_fetch_array($result)) {
                    $columns[] = $array_data[0];
                }
            }
        }
        // Returns the array
        return $columns;
    }

Usage Example

Exemple #1
0
    echo $row->Color . " - " . $row->Age . "<br />\n";
}
// --- Loop through the records with an index -----------------------
for ($index = 0; $index < $db->RowCount(); $index++) {
    $row = $db->Row($index);
    echo "Row " . $index . ":" . $row->Color . " - " . $row->Age . "<br />\n";
}
// --- We can even grab array data from the last result set ---------
$myArray = $db->RecordsArray();
// --- List all of the tables in the database -----------------------
$tables = $db->GetTables();
foreach ($tables as $table) {
    echo $table . "<br />\n";
}
// --- Show the columns (field names) in a table --------------------
$columns = $db->GetColumnNames("test");
foreach ($columns as $column) {
    echo $column . "<br />\n";
}
// --- Find a column (field) type and length ------------------------
echo "Type: " . $db->GetColumnDataType("Color", "Test") . "<br />\n";
echo "Length: " . $db->GetColumnLength("Color", "Test") . "<br />\n";
// --- Get a column's ordinal position (the column number) ----------
echo $db->GetColumnID("Age", "Test") . "<br />\n";
// --- Check for errors ---------------------------------------------
if ($db->Error()) {
    echo "<h3>" . $db->Error() . "</h3>\n";
} else {
    echo "<p>There were no errors</p>\n";
}
// --- Format some values ready for SQL -----------------------------