In my table, I have a userID that automatically increments. On the same line, I have idHash. Is it possible to generate idHash (just the sum of MD5) from it directly with the same INSERT statement so that I do not need to select an identifier and then update idHash again?
The problem is that I do not know the user ID before it is generated (automatically added) by MySQL.
Thanks Frank
PS: I am using PHP. PPS: This question concerns a single ONE INSERT. I know that I can use PHP or other languages to manually select data and then update it.
, INSERT.
INSERT
, , , , INSERT, , .
PHP-
<? $tablename = "tablename"; $next_increment = 0; $qShowStatus = "SHOW TABLE STATUS LIKE '$tablename'"; $qShowStatusResult = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus ); $row = mysql_fetch_assoc($qShowStatusResult); $next_increment = $row['Auto_increment']; echo "next increment number: [$next_increment]"; ?>
, .
. ( , 2 )
: http://blog.jamiedoris.com/geek/560/
, , . :
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "INSERT INTO users VALUES (....)"; $mysqli->query($query); $newUserID = $mysqli->insert_id; $query = "UPDATE users SET idHash = MD5(userID) WHERE userID = $newUserID"; $mysqli->query($query); /* close connection */ $mysqli->close(); ?>
AFAIK "" , auto_increment.
auto_increment
, , :
insert into mytable (col1, col2, col3, idhash) values ('', '', '', md5(select max(id) from mytable))
, id, id ?
id
, :
CREATE TABLE tbl (id INT PRIMARY KEY AUTO_INCREMENT, idHash TEXT); INSERT INTO tbl (idHash) VALUES (MD5(LAST_INSERT_ID() + 1)); SELECT *, MD5(id) FROM tbl;
, , LAST_INSERT_ID .
LAST_INSERT_ID
MD5(column_name) auto_increment , , MD5(0).
MD5(column_name)
MD5(0)