简体   繁体   中英

INSERT into table if not exists and return result

I have a 'users' SQL table structure like this (the ID is randomly generated for certain reasons, it is not auto-incremented):

ID    name     deleted    lastActive
3242  Joe      0          20-6-2012 23:14
2234  Dave     0          20-6-2012 23:13
2342  Simon    1          20-6-2012 23:02
9432  Joe      1          20-6-2012 22:58

In one query (to avoid concurrent queries adding the same name twice), I need to add a new user to the table IF there is not already a record with that name AND deleted = 0. I then need to know the result of the query (if the user was added) so that I can report back saying if the user was added or not. Is this possible using PHP?

I could do this (but as a prepared statement, of course!):

INSERT INTO users (ID, name) VALUES ($id, $name) 
    WHERE NOT EXISTS (SELECT 1 FROM users WHERE name = $name AND deleted = 0)

But how can I know if the user was added or not?

If you're using mysqli, you can use the mysqli_stmt_affected_rows() function to determine how many rows were inserted.

Similarly, you can use the PDOStatement::rowCount() method to determine how many rows were inserted for PDO.

Both functions will tell you the number of rows that were inserted as a result of the query.

Here's a nice insertion method that returns the ID:

/**
 * Execute an insert or update in the database.
 * @param $table - Table name.
 * @param $key_name - Primary key to update. NULL to a insert
 * @param $data - Column data array
 * @param $call_on_error function name that should called in case of an exception during the
 * execution of the statment, the function is expected to take one argument, the exception object
 * @return mixed An array containing the key inserted or updated on success, false on failure.
 */
function INSERT($table, $key_name, &$data, $call_on_error = null) {
    list($min_cols, $prefix, $suffix, $key_value) = isset($data[$key_name]) ?
        array(2, 'UPDATE', " WHERE `$key_name`=:$key_name", $data[$key_name]) :
        array(1, 'INSERT', '', null);
    if (count($data) < $min_cols) {
        return false;
    }
    $set_clause = '';
    foreach ($data as $k => $v) {
        if ($k !== $key_name) {
            if (($flag_name = strstr($k, "__", true))) {
                if (strcmp($k, "{$flag_name}__mask") && isset($data["{$flag_name}__value"]))
                    $set_clause .= ",`$flag_name`=:{$flag_name}__value | (`$flag_name` & :{$flag_name}__mask)";
            } else {
                $set_clause .= ",`$k`=:$k";
            }
        }
    }
    global $dbo_error_duplicated;
    $dbo_error_duplicated = false;
    $dbh = DBH();
    try {
        $sth = $dbh->prepare("$prefix $table SET " . substr($set_clause, 1) . $suffix);
        $res = $sth->execute($data);
    } catch (PDOException $e) {
        $dbo_error_duplicated = $sth->errorCode() === '23000';
        echo $e;
        if(isset($call_on_error)){
            call_user_func($call_on_error, $e);
        }
        $res = false;
    }
    if ($res) {
        if ($key_value === null && is_numeric($id = $dbh->lastInsertId())) {
            $key_value = $id;
        }
        $res = $key_value === null ? false : array($key_name => $key_value);
    }
    return $res;
}

And… the DBH config:

/**
 * Get Data Base Handler.
 * Manual @ http://www.php.net/manual/en/pdostatement.fetch.php
 * More info @ http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
 *
 * @return PDO Data Base Handler
 */
function DBH() {
    global $DBH;
    global $db_config;

    if ($DBH === null) {
        // throws PDOException on connection error
        $DBH = new PDO("mysql:host=$db_config[host];dbname=$db_config[dbname]", $db_config['user'], $db_config['pass'],
                        array(PDO::ATTR_PERSISTENT => $db_config['persistent'], PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '$db_config[encoding]'"));
        // ask PDO to throw exceptions for any error
        $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    return $DBH;
}

That uses this .ini file:

[db_config]
persistent  = true
host        = "localhost"
user        = "root"
pass        = ""
dbname      = "theDbName"
# host      = "db.production_host.com"
# user      = "prod_root"
# pass      = "big4nd5tr0ngp4s5word"
# dbname    = "theDbName"
encoding    = "UTF8"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM