MySQL::GetColumnDataType PHP Method

GetColumnDataType() public method

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

Usage Example

Example #1
0
    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 -----------------------------
// You do not have to create the object to use these. Simply include
// the class in your PHP file. These are called "Static" methods.
echo "<br /><br />\n\n";
echo MySQL::SQLValue("Let's format some text") . "<br />\n";
echo MySQL::SQLValue(date("m/d/Y"), MySQL::SQLVALUE_DATE) . "<br />\n";