MySQL::GetColumnID PHP Method

GetColumnID() public method

This function returns the position of a column
public GetColumnID ( 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 Column ID
    public function GetColumnID($column, $table = "")
    {
        $this->ResetError();
        $columnNames = $this->GetColumnNames($table);
        if (!$columnNames) {
            return false;
        } else {
            $index = 0;
            $found = false;
            foreach ($columnNames as $columnName) {
                if ($columnName == $column) {
                    $found = true;
                    break;
                }
                $index++;
            }
            if ($found) {
                return $index;
            } else {
                $this->SetError("Column name not found", -1);
                return false;
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
$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";
// --- Format some values ready for SQL based on a boolean value ----
echo MySQL::SQLBooleanValue(false, "1", "0", MySQL::SQLVALUE_NUMBER);