Skip to content

Commit

Permalink
Added logic to insert method of mysql to return the auto-inc ID on ta…
Browse files Browse the repository at this point in the history
…bles when using on duplicate key update
  • Loading branch information
Chris DeGroat committed Sep 16, 2014
1 parent ea9d3ad commit a06e030
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions mysql.php
Expand Up @@ -8,6 +8,7 @@ class mysql
static private $errors = array();
static private $debug_mode = FALSE;
static private $queries = array();
static private $last_query = NULL;

public static function database_config($alias, $host, $database, $username, $password)
{
Expand All @@ -20,7 +21,7 @@ public static function database_config($alias, $host, $database, $username, $pas

}

public static function insert($alias, $table, $values, $on_dupkey_update = array())
public static function insert($alias, $table, $values, $on_dupkey_update = array(), $auto_inc_column = FALSE)
{
$sql = "INSERT INTO {$table} ( " . implode(', ', array_keys($values)) . " ) VALUES ( " . implode(', ', array_map(array('mysql','quote'),$values)) . " ) ";
if(!empty($on_dupkey_update) && is_array($on_dupkey_update))
Expand All @@ -31,6 +32,10 @@ public static function insert($alias, $table, $values, $on_dupkey_update = array
{
$sql_parts[] = " {$key} = " . mysql::quote($val);
}
if($auto_inc_column)
{
$sql_parts[] = "{$auto_inc_column} = LAST_INSERT_ID( {$auto_inc_column})";
}
$sql .= implode(", ", $sql_parts);
}

Expand All @@ -39,7 +44,6 @@ public static function insert($alias, $table, $values, $on_dupkey_update = array
return self::last_insert_id($alias);
}

print $sql; exit;
return FALSE;
}

Expand Down Expand Up @@ -78,6 +82,7 @@ public static function query($alias, $sql, $cache = FALSE, $update_cache = FALSE
{
self::$queries[] = $sql;
}
self::$last_query = $sql;

// Execute our query and get the result
$result = mysqli_query($connection, $sql);
Expand Down Expand Up @@ -229,6 +234,11 @@ public static function get_queries()
return self::$queries;
}

public static function get_last_query()
{
return self::$last_query;
}

// ==========================================================

private static function validate_alias($alias)
Expand Down

0 comments on commit a06e030

Please sign in to comment.