php - MySQLI - INSERT or UPDATE on duplicate, multiple users -
php - MySQLI - INSERT or UPDATE on duplicate, multiple users -
currently update table next query.
$query = "update `my_table` set `views` = `views` + 1 `user_id` in (".$users.")"; this updates users within table +1 on respective views column value.
i need insert new row if new day. found (via stack overflow) on duplicate key update function. working not exclusively sure on how multiple users within single query (like query above).
the current query have far following, limited single user
$query = "insert `my_table` (`user_id`, `day`, `listing`) values (12, 2, 1) on duplicate key update `views`=`views`+1"; i not wish set query within php foreach loop perform given number of rows (40 - 100)
you can execute multiple queries using mysql transactions: http://www.php.net//manual/en/pdo.transactions.php
function executetransaction($sqlstmts,$params){ try{ $conn = new pdo('mysql:host=localhost;dbname=test', $user, $pass); $conn->begintransaction(); $n = count($sqlstmts); for($i=0; $i<$n; $i++){ $stmt = $conn->prepare($rawstmts[$i]); $stmt->execute($params[$i]); } $conn->commit(); } catch(pdoexception $ex){ $conn->rollback(); homecoming false; } catch(exception $ex){ $conn->rollback(); homecoming false; } homecoming true;} here each entry in $sqlstmts i.e., $sqlstmts[$i] prepared statement , each $params i.e., $params[$i] array of parameters prepared statement $sqlstmts[$i]
but if want execute them @ time, should like
$query = "insert `my_table` (`user_id`, `day`, `listing`) values (12,2,1),(13,3,2),(14,4,3) on duplicate key update `views`=`views`+1"; php mysql sql mysqli
Comments
Post a Comment