2

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

我的$msqliobj 可以访问数据库

但是我对这个错误有点难过。我被告知该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
}

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

** * ** * ** * ** * ** * ** * **已解决 (?)** * ** * ** * ** * ** * ** *

固定(或似乎是):

引用自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。无论如何,对于未来的用户,我希望这会有所帮助。

4

1 回答 1

0

有点挑剔,但明智的做法是对 $_SESSION['user_id'] 输入执行诸如添加斜杠或 mysql_real_escape_string 之类的操作以避免注入问题。

于 2013-07-20T02:04:19.230 回答