MySQL表锁定问题

时间:2013-07-15 09:05:05

标签: php mysql locking

我认为这可能是一个死锁问题,但我只是在阅读表数据。或者我看不到的非常简单的东西。

我的$msqli obj适用于数据库访问

但我对这个错误感到有点难过.. 我被告知clients表未被LOCK TABLES

锁定

欢迎任何建议..谢谢 这是我的PHP代码

$mysqli = $GLOBALS['mysqli'];
$mysqli->query("LOCK TABLES clients READ, invoices READ, estimates READ");

$mysqli->query("SET @inv='Invoice'");
$mysqli->query("SET @est='Estimate'");

$sql = " (SELECT @inv, name AS client, invoices.id, invoices.ref AS ref, invoices.addTs AS ts \n"
        . "FROM `clients` , `invoices` \n"
        . "WHERE invoices.user_id =  " . $_SESSION['user_id'] . " \n"
        . "AND clients.id = invoices.client_id)\n"
        . "UNION ALL\n"
        . "(SELECT @est, name as client, estimates.id, estimates.ref AS ref, estimates.addTs AS ts \n"
        . "FROM `clients` ,`estimates` \n"
        . "WHERE estimates.user_id =  " . $_SESSION['user_id'] . " \n"
        . "AND clients.id = estimates.client_id) \n"
        . "ORDER BY ts DESC LIMIT 5";

if ($result = $mysqli->query($sql) or die(mysqli_error($mysqli))) {
//Do stuff here
}

当我在PhpMyAdmin中运行此$sql查询时,它成功了... 谢谢你的帮助..

** * ** * ** * 的** * ** * ** * ** 已解决(?) ** * ** * ** * ** * ** < EM> * ** *

已修复(或似乎是):

来自http://bugs.mysql.com/bug.php?id=6588

  

“你不能在一个查询中多次使用锁定表 - 为此使用别名。”

因此查询更新为

    $mysqli->query("LOCK TABLES clients_t READ,clients READ ,
                                invoices READ, estimates READ");
$mysqli->query("SET @inv='Invoice'");
$mysqli->query("SET @est='Estimate'");

$sql = " " .
        "(SELECT @inv, name AS client, invoices.id, 
                  invoices.ref AS ref, invoices.addTs AS ts "

        . "FROM `clients` AS clients_t , `invoices`\n"
        . "WHERE invoices.user_id =  " . $_SESSION['user_id'] . " \n"
        . "AND clients_t.id = invoices.client_id)\n"
        . "UNION ALL \n"
        . "(SELECT @est , name AS client, estimates.id, 
                   estimates.ref AS ref, estimates.addTs AS ts\n"
        . "FROM `clients` ,`estimates` \n"
        . "WHERE estimates.user_id =  " . $_SESSION['user_id'] . "  \n"
        . "AND clients.id = estimates.client_id)\n"
        . "ORDER BY ts DESC LIMIT 5";

我还需要锁定别名表clients_t。 无论如何对于未来的用户我希望这会有所帮助

1 个答案:

答案 0 :(得分:0)

有点挑剔,但对于$ _SESSION [&#39; user_id&#39;]输入执行诸如addslashes或mysql_real_escape_string之类的操作是明智的,以避免注入问题。