这是我的php代码
$LockQuery = "LOCK Table `customers` WRITE";
$LockResult= mysqli_query($connection,$LockQuery);
当我运行此脚本时,它不会锁定客户表,但会在 mysql 控制台中锁定。它在 phpmyadmin 中也不起作用。 那有什么理由吗? 这是 phpmyadmin 示例
但是
最佳答案
来自mysql manual :
If the connection for a client session terminates, whether normally or abnormally, the server implicitly releases all table locks held by the session (transactional and nontransactional). If the client reconnects, the locks will no longer be in effect.
当您在脚本中打开数据库连接时,您(或 phpMyAdmin)会启动一个 session 。当您的或 phpMyAdmin 的脚本完成时,数据库连接会自动关闭并且 session 终止。
因此,如果您的脚本只运行很短的时间,您将不会注意到锁定,因为它几乎是立即释放的。
要测试您的代码是否有效,您必须打开一个数据库连接,执行 lock 语句并添加一个 sleep(60);
(例如...)以保持锁定一分钟。
关于php - 通过php锁定表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389700/