用PHP锁定MySQL表 [英] Lock mysql table with php

查看:93
本文介绍了用PHP锁定MySQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脚本1.

$query_ = "lock tables test1 as test11 write";
mysql_query($query);
$query_ = "select * from test11";
sleep(20);
$query_ = "unlock tables";
mysql_query($query_);

脚本2.

$query_ = "select * from test1";
$result = mysql_query($query_);

问题是,如果我在运行第一个脚本的同时运行第二个脚本. 表未锁定.而且我可以从中读取任何数据.

The problem is that if i run second script while running first script. Table is not locked. And i can read any data from it.

我需要将其锁定并返回错误.

I need it to be locked and return error.

如何进行这项工作?

推荐答案

您正在read$query_ = "lock tables test1 as test11 read";-锁定表,这意味着其他查询仍然可以读取表而没有任何问题(相关链接-向下滚动至有关锁类型的部分):

You are read locking the table with $query_ = "lock tables test1 as test11 read";- which means that other queries can still read it without any problems what-so-ever (Relevant link - scroll down to the section on lock types):

read锁类型的信息:

  • 持有锁的会话可以读取表(但不能写入表).
  • 多个会话可以同时获取该表的READ锁.
  • 其他会话可以在不显式获取READ锁的情况下读取表.

如果要停止其他任何操作,除了要重定义表外,还需要使用write锁,如下所示:

If you want to stop anything else so much as reding the table, you need to use a write lock as follows:

$query_ = "lock tables test1 as test11 write";

这篇关于用PHP锁定MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆