douzhuan1467 2013-03-27 20:47
浏览 37
已采纳

第一个查询中返回的第二个查询的结果

In my code, I am checking whether a value exists in the db. If it does exists, and error should be returned.. however, if it doesn't... it should create the value in the db. This is my code:

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

    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 "

";
    echo "Rows: " . $rQur->num_rows;
    echo "

";

    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);
    }
}

This is the output when I run the command from my 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>

How can it be 1 row, if there is no data in the table prior to the call being run? The method is only run once, and the insert is done After the select is preformed.

Edit 1
If I comment out the Insert SQL row, it doesn't add anything to the DB.

  • 写回答

1条回答 默认 最新

  • douyi9705 2013-03-27 21:55
    关注

    Seems that autocommit was default set to True. I switched it to false using mysqli_autocommit($this, false); in my subclass of MySqli and it solved my problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源