MySQL Replace Manual

http://dev.mysql.com/doc/refman/5.0/en/replace.html

PHP PDO Insert Statement

http://blog.teknober.com/2011/03/06/php-pdo-generate-insert-statement/

PHP PDO Update Statement

http://blog.teknober.com/2011/03/06/php-pdo-generate-update-statement/

Sample:

$model = new PDO(/* enter your parameters */);

$statement = generateReplaceSet("mytable", new TModelColumn("name", "Teknober", PDO::PARAM_STR, 100), null, true);

PDO Statement: Replace

function generateReplaceSet($table, $columns = null, $options = null, $execute = false) {
    if (isset($columns) == true) {
        /* prepare result */
        $result = null;

        /* prepare sql */
        $sql = "
                REPLACE `%s` INTO
                    %s (%s)
                VALUES
                    (%s);";

        /* prepare columns_string */
        $columns_string = null;
        $values_string = null;

        if (is_array($columns) == true) {
            /* iterate columns to generate columns_string */
            foreach ($columns as $value) {
                $columns_string .= "`" . $value->getKey() . "`,";
                $values_string .= "?,";
            }

            /* remove trailing comma */
            $columns_string = substr($columns_string, 0, strlen($columns_string) - 1);
            $values_string = substr($values_string, 0, strlen($values_string) - 1);
        } elseif ($columns instanceof TModelColumn) {
            $columns_string = "`" . $columns->getKey() . "`";
            $values_string = "?";
        } else {
            trigger_error("No column is defined in set", E_ERROR);
        }

        /* prepare sql */
        $sql = sprintf($sql, $options, $table, $columns_string, $values_string);

        /* prepare statement */
        $result = $model->prepare($sql);

        /* prepare statement parameter index */
        $param_index = 0;

        /* bind statement parameters */
        if (is_array($columns) == true) {
            foreach ($columns as $column) {
                $result->bindParam(++$param_index, $column->getValue(), $column->getType(), $column->getLength());
            }
        } elseif ($columns instanceof TModelColumn) {
            $result->bindParam(++$param_index, $columns->getValue(), $columns->getType(), $columns->getLength());
        } else {
            trigger_error("No column is defined in set", E_ERROR);
        }

        /* clean up */
        unset($table, $columns, $options, $sql, $columns_string, $values_string, $param_index);

        /* return PDO Statement */
        if ($execute) {
            return $result->execute();
        } else {
            return $result;
        }
    } else {
        trigger_error("No column is defined in arguments");
    }
}

TModelColumn Class

class TModelColumn {
    public $key = null;
    public $value = null;
    public $type = null;
    public $length = null;
    public function __construct($key, $value = null, $type = null, $length = null) {
        $this->key = $key;
        $this->value = $value;
        $this->type = $type;
        $this->length = $length;
    }
    public function getKey() {
        return $this->key;
    }
    public function getValue() {
        return $this->value;
    }
    public function getType() {
        return $this->type;
    }
    public function getLength() {
        return $this->length;
    }
}
$options,