MySQL::SQLValue PHP Method

SQLValue() public static method

[STATIC] Formats any value into a string suitable for SQL statements (NOTE: Also supports data types returned from the gettype function)
public static SQLValue ( mixed $value, string $datatype = self::SQLVALUE_TEXT ) : string
$value mixed Any value of any type to be formatted to SQL
$datatype string Use SQLVALUE constants or the strings: string, text, varchar, char, boolean, bool, Y-N, T-F, bit, date, datetime, time, integer, int, number, double, float
return string
    public static function SQLValue($value, $datatype = self::SQLVALUE_TEXT)
    {
        $return_value = "";
        switch (strtolower(trim($datatype))) {
            case "text":
            case "string":
            case "varchar":
            case "char":
                if (strlen($value) == 0) {
                    $return_value = "NULL";
                } else {
                    if (get_magic_quotes_gpc()) {
                        $value = stripslashes($value);
                    }
                    $return_value = "'" . str_replace("'", "''", $value) . "'";
                }
                break;
            case "number":
            case "integer":
            case "int":
            case "double":
            case "float":
                if (is_numeric($value)) {
                    $return_value = $value;
                } else {
                    $return_value = "NULL";
                }
                break;
            case "boolean":
                //boolean to use this with a bit field
            //boolean to use this with a bit field
            case "bool":
            case "bit":
                if (self::GetBooleanValue($value)) {
                    $return_value = "1";
                } else {
                    $return_value = "0";
                }
                break;
            case "y-n":
                //boolean to use this with a char(1) field
                if (self::GetBooleanValue($value)) {
                    $return_value = "'Y'";
                } else {
                    $return_value = "'N'";
                }
                break;
            case "t-f":
                //boolean to use this with a char(1) field
                if (self::GetBooleanValue($value)) {
                    $return_value = "'T'";
                } else {
                    $return_value = "'F'";
                }
                break;
            case "date":
                if (self::IsDate($value)) {
                    $return_value = "'" . date('Y-m-d', strtotime($value)) . "'";
                } else {
                    $return_value = "NULL";
                }
                break;
            case "datetime":
                if (self::IsDate($value)) {
                    $return_value = "'" . date('Y-m-d H:i:s', strtotime($value)) . "'";
                } else {
                    $return_value = "NULL";
                }
                break;
            case "time":
                if (self::IsDate($value)) {
                    $return_value = "'" . date('H:i:s', strtotime($value)) . "'";
                } else {
                    $return_value = "NULL";
                }
                break;
            default:
                exit("ERROR: Invalid data type specified in SQLValue method");
        }
        return $return_value;
    }

Usage Example

Example #1
1
function insertKiiconnect()
{
    global $databaseKiiconnect;
    $data = json_decode(stripslashes($_POST["data"]));
    $update["nombre"] = MySQL::SQLValue($data->nombre);
    $update["tag"] = MySQL::SQLValue($data->tag);
    $update["descripcion"] = MySQL::SQLValue($data->descripcion);
    $update["icono"] = MySQL::SQLValue($data->icono);
    $update["link"] = MySQL::SQLValue($data->link);
    $update["orden"] = MySQL::SQLValue($data->orden);
    $update["id_categoria"] = MySQL::SQLValue($data->id_categoria);
    $update["activo"] = MySQL::SQLValue($data->activo, MySQL::SQLVALUE_NUMBER);
    $databaseKiiconnect->InsertRow("kiiconnect_setting", $update);
    echo json_encode(array("success" => $databaseKiiconnect->ErrorNumber() == 0, "msg" => $databaseKiiconnect->ErrorNumber() == 0 ? "Parametro insertado exitosamente" : $databaseKiiconnect->ErrorNumber(), "data" => array(array("id" => $databaseKiiconnect->GetLastInsertID(), "nombre" => $data->nombre, "tag" => $data->tag, "descripcion" => $data->descripcion, "icono" => $data->icono, "link" => $data->link, "orden" => $data->orden, "id_categoria" => $data->id_categoria, "activo" => $data->activo))));
    //inserto como blob la imagen
    $file = __DIR__ . '/../../../../' . $data->icono;
    if ($fp = fopen($file, "rb", 0)) {
        $picture = fread($fp, filesize($file));
        fclose($fp);
        // base64 encode the binary data, then break it
        // into chunks according to RFC 2045 semantics
        $base64 = chunk_split(base64_encode($picture));
        $tag = 'data:image/png;base64,' . $base64;
    }
    $lastId = $databaseKiiconnect->GetLastInsertID();
    $databaseKiiconnect->Query("update kiiconnect_setting set file='{$tag}'  where `id`='{$lastId}'");
}
All Usage Examples Of MySQL::SQLValue