2

在我的代码中,我正在检查数据库中是否存在值。如果确实存在,则应返回错误。但是,如果不存在,则应在数据库中创建该值。这是我的代码:

/**
 * Check if the string has been used before
 *
 * @return bool
 */
private function _checkStringBeenUsed ()
{
    echo "Running check!\n";

    if ($this->_sString === null) {
        parent::showErrorMessage('Invalid shared String', 1005);
    }

    $this->_validateString();

    $sSql     = "SELECT * FROM operator_string WHERE
                    operatorId = '{$this->_sOperator}' AND
                    sharedString = '{$this->_sString}' AND
                    time >= DATE_SUB(
                        NOW(),
                        INTERVAL 1 DAY
                    )";
    $rQur     = $this->_oDb->query($sSql);

    echo $sSql;
    echo "\n\n";
    echo "Rows: " . $rQur->num_rows;
    echo "\n\n";

    if ($rQur->num_rows > 0) {
        parent::showErrorMessage('Shared string already used', 1005);
    } else {
        $sSql = "INSERT INTO operator_string
                    (operatorId, sharedString, time, hash)
                    VALUES
                    ('{$this->_sOperator}',
                        '{$this->_sString}',
                        NOW(),
                        '{$this->_sHash}'
                    )";
        $this->_oDb->query($sSql);
    }
}

/**
 * Validates the string and the hash
 *
 * @return void
 */
private function _validateString ()
{
    $sHashString = $this->_sOperator . $this->_sSharedSecret . $this->_sString;
    $sHash = hash('sha256', $sHashString);

    if ($sHash != $this->_sHash) {
        parent::showErrorMessage('Invalid authentication hash send', 1006);
    }
}

这是我从 iPhone 运行命令时的输出:

2013-03-27 21:43:08.560 MyApp[45315:c07] Result: Running check!
SELECT * FROM operator_string WHERE
                        operatorId = 'a8198231u82' AND
                        sharedString = 'cqCEYGIbNvF7HU1' AND
                        time >= DATE_SUB(
                            NOW(),
                            INTERVAL 1 DAY
                        )

Rows: 1

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE error>
<error>
    <errorCode>1005</errorCode>
    <errorMessage>Shared string already used</errorMessage>
</error>

如果在运行调用之前表中没有数据,它怎么可能是 1 行?该方法只运行一次,插入完成执行选择。

编辑 1
如果我注释掉插入 SQL 行,它不会向数据库添加任何内容。

4

1 回答 1

0

似乎自动提交默认设置为True. mysqli_autocommit($this, false);我在我的子类中将其切换为 falseMySqli并解决了我的问题。

于 2013-03-27T21:55:29.257 回答