[英]how to get the value of next autoincrements in php for mysql
ok so I have to run 2 updates that rely on each others IDs to sync properly. 好吧,所以我必须运行2个更新,依赖于其他ID正确同步。
conversation ID has to be in message table, where as MessageID has to be in the conversation table. 会话ID必须位于消息表中,其中MessageID必须位于会话表中。 and both are auto increment values.
两者都是自动增量值。
mysql_query("UPDATE ow_base_user Set activityStamp = '$stamp' where id = '$profile_id' ");
mysql_query("INSERT INTO ow_base_user_online (id, userId, activityStamp, context) VALUES ('', '$profile_id', '$stamp', '1')");
$conid = mysql_query("SELECT ID AS id FROM ow_mailbox_conversation WHERE ID = IDENT_CURRENT") + 1;
$msgid = mysql_query("SELECT ID AS id FROM ow_mailbox_message WHERE ID = IDENT_CURRENT") + 1;
mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('$conid', '$profile_id', '637', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '$msgid', '$stamp')");
mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
VALUES ('$msgid', '$conid', '$stamp', '$profile_id', '637', 'hi there', '0', '0', '1')");
I thought possibly I could use IDENT_CURRENT + 1 to get the ID but when I echo it nothing comes up. 我想可能我可以使用IDENT_CURRENT + 1来获取ID,但是当我回应它时,没有任何东西出现。 sorry a bit new at this still can
对不起有点新鲜还可以
--- edit --- ---编辑---
so tried using insert_id - problem then is the first number did not come back correctly. 所以尝试使用insert_id - 问题然后是第一个数字没有正确回来。 gave me a number almost 2x as large as it should be.
给了我一个几乎是应有的2倍的数字。
here is the code 这是代码
mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', '$profile_id', '637', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '', '$stamp')");
$conid = mysql_insert_id();
mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
VALUES ('', '$conid', '$stamp', '$profile_id', '637', 'hi there', '0', '0', '1')");
$msgid = mysql_insert_id();
mysql_query("UPDATE ow_mailbox_conversation Set lastMessageId = '$msgid' where id = '$conid' ");
--- edit --- ---编辑---
here is my full code everything seems to be working fine except Convarsation string is not inserting, then the $conid comes back really high in the debug. 这里是我的完整代码一切似乎工作正常除了Convarsation字符串没有插入,然后$ conid在调试中回来真的很高。
$link = mysql_connect($OW_DB_HOST, $OW_DB_USER, $OW_DB_PASS);
if (!$link) {
die('Connection fail: ' . mysql_error());
}
mysql_select_db($OW_DB_NAME, $link);
// End of connection database
$stamp = time();
//1
mysql_query("UPDATE ow_base_user Set activityStamp = '$stamp' where id = '$profile_id' ");
mysql_query("INSERT INTO ow_base_user_online (id, userId, activityStamp, context) VALUES ('', '$profile_id', '$stamp', '1')");
$receiver_id = '637';
mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', '$profile_id', '$receiver_id', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '', '$stamp')");
$conid = mysql_insert_id();
mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
VALUES ('', '$conid', '$stamp', '$profile_id', '$receiver_id', 'hi there', '0', '0', '1')");
$msgid = mysql_insert_id();
mysql_query("INSERT INTO ow_mailbox_last_message (id, conversationId, initiatorMessageId, interlocutorMessageId)
VALUES ('', '$conid', '$msgid', '0')");
$lastmsgid = mysql_insert_id();
mysql_query("UPDATE ow_mailbox_conversation Set lastMessageId = '$lastmsgid' where id = '$conid' ");
//End of script if devmode = false
// Output all used variables on devmode = true
if (DEVMODE){
echo 'Connection ok';echo '<br>';
echo '1 = ',$conid;echo '<br>';
echo '2 = ',$msgid;echo '<br>';
echo '3 = ',$lastmsgid;echo '<br>';
}
// End of testbench
mysql_close($link);
?>
you can use PDO::lastInsertId for getting id of last inserted row if you used PDO for connection. 如果使用PDO进行连接,可以使用PDO :: lastInsertId获取最后插入行的id。 please refer: http://php.net/manual/en/pdo.lastinsertid.php
请参考: http : //php.net/manual/en/pdo.lastinsertid.php
alternatives, http://php.net/manual/en/mysqli.insert-id.php 替代方案, http://php.net/manual/en/mysqli.insert-id.php
You cannot be sure of the next insert ID because your system will (I assume) be handling multiple feeds at a time as your site is not single-user 您无法确定下一个插入ID,因为您的系统将(我假设)一次处理多个Feed,因为您的站点不是单用户
The best thing to do is to use three queries: 最好的办法是使用三个查询:
For this, I recommend using the InnoDB table and transactions. 为此,我建议使用InnoDB表和事务。 In the event of a failure, you can roll back to the state before anything started and minimise IDs being present which are unattached.
如果发生故障,您可以回滚到任何启动之前的状态,并最小化存在的未附加的ID。
An alternative is to create a third table to store the relationship between the tables. 另一种方法是创建第三个表来存储表之间的关系。 Essentially, it stores id1 and id2 and nothing else
从本质上讲,它存储id1和id2而不存储任何其他内容
If possible, re-work your tables so that this mutual referencing is not required. 如果可能,请重新处理表,以便不需要此相互引用。 It will make things easier in the long run
从长远来看,这将使事情变得更容易
Finally, stop using the mysql_
functions. 最后,停止使用
mysql_
函数。 They are deprecated and removed completely in PHP 7. Use the PDO
or mysqli_
functions. 它们在PHP 7中被弃用并完全删除。使用
PDO
或mysqli_
函数。 You'll thank me 你会感谢我的
the problem was errors with the structure 问题是结构错误
here is the fixed code. 这是固定代码。
mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, `read`, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', $profile_id, $receiver_id, 'mailbox_chat_conversation', 1, 0, 1, 0, $stamp, 0, 0, '', $stamp)");
$conid = mysql_insert_id();
notice the removed ' ' around the numbers and variables. 注意数字和变量周围的删除''。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.