MySQL::GetColumnLength PHP Method

GetColumnLength() public method

This function returns the field length or returns FALSE on error
public GetColumnLength ( string $column, string $table = "" ) : integer
$column string Column name
$table string (Optional) If a table name is not specified, the last returned records are used.
return integer Field length
    public function GetColumnLength($column, $table = "")
    {
        $this->ResetError();
        if (empty($table)) {
            if (is_numeric($column)) {
                $columnID = $column;
            } else {
                $columnID = $this->GetColumnID($column);
            }
            if (!$columnID) {
                return false;
            } else {
                $field = mysqli_fetch_field_direct($this->last_result, $columnID);
                if (!$field) {
                    $this->SetError();
                    return false;
                } else {
                    return $field->length;
                }
            }
        } else {
            $records = mysqli_query($this->mysql_link, "SELECT " . $column . " FROM " . $table . " LIMIT 1");
            if (!$records) {
                $this->SetError();
                return false;
            }
            $field = mysqli_fetch_field_direct($records, 0);
            if (!$field) {
                $this->SetError();
                return false;
            } else {
                return $field->length;
            }
        }
    }

Usage Example

Esempio n. 1
0
}
// --- 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";
echo MySQL::SQLValue(123, MySQL::SQLVALUE_NUMBER) . "<br />\n";