使用php错误锁定mysql中的表
我正在尝试为本地托管的示例购物网站在我的数据库上实现一个表锁 以下是my index.php的示例:使用php错误锁定mysql中的表,php,mysql,locking,Php,Mysql,Locking,我正在尝试为本地托管的示例购物网站在我的数据库上实现一个表锁 以下是my index.php的示例: case "add": if(!empty($_POST["quantity"])) { if(!isset($timestamp)){ $timestamp = time(); $lock = $db_handle->runQuery("LOCK TABLE orders_f write,
case "add":
if(!empty($_POST["quantity"])) {
if(!isset($timestamp)){
$timestamp = time();
$lock = $db_handle->runQuery("LOCK TABLE orders_f write, prod_f read");
}
$productByCode = $db_handle->runQuery("SELECT * FROM prod_f WHERE code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
表锁已被强制执行的位置。当我尝试添加到购物车中时,出现以下错误,这是上面case语句的来源:
警告:mysqli_fetch_assoc()希望参数1是mysqli_结果,布尔值在第24行的path\dbcontroller.php中给出
以下是my dbcontroller.php的一个片段:
我整晚都在Youtube和Stackoverflow上搜索,但似乎没有一个选项起作用。它位于
Apache
服务器上,在64位windows10
机器上使用Wamp。欢迎您提出任何建议。为什么首先要使用表锁?这是一项要求,当用户向购物车添加物品时,要防止其他用户在10分钟内访问产品表。在异常模式下使用mysqli,以获取有用的错误消息:好的,我会尝试一下,并告诉您我同意@YuriG,把桌子锁上10分钟真是太过分了。这实际上意味着您一次只能有一个客户的购物车中有任何东西。除非这是你任务中的一个具体要求,否则我会忘记这一点。
function runQuery($query) {
$result = mysqli_query($this->conn,$query);
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
else
return false;
}