使用多个参数准备SQL查询(mysqli)

时间:2016-05-10 23:24:34

标签: php sql mysqli prepared-statement bindparam

我不知道为什么准备这个sql查询无效。

$from = 1462890333;
$to = 1462890335;
$token = 123;
$sql = "SELECT temperature FROM `am_measurements` WHERE token = ? AND date BETWEEN ? AND ?";
if($stmt = $conn->prepare($sql)){
    $stmt->bind_param('sii', $token, $from, $to);
    $stmt->execute();
    $stmt->bind_result($a);
    $stmt->fetch();
    echo $a;
} else {
    echo "not good";
}

结果是“不好”。应该如何做?

以下是am_measurements表中的数据: https://mega.nz/#!tJEx0IiL!OoXHSVJh0A0kAVzYrcJKL6BGxktcZZWpOC26KV-Egao

@edit

现在我知道是什么导致了这个问题,但我不知道为什么以及如何解决它:

$sensor_id = 0;
$from = 1462890333;
$to = 1462890335;

//MySQL Database Connect
include 'database.php';

$stmt = $conn->stmt_init();
$stmt = $conn->prepare("SELECT token FROM `am_sensors` WHERE id = ?");
$stmt->bind_param('i', $sensor_id);
$stmt->execute();
$stmt->bind_result($token);
$stmt->fetch();
echo $token; //this returns "123"
$sql = "SELECT temperature FROM `am_measurements` WHERE token = ? AND date BETWEEN ? AND ?";
if($stmt = $conn->prepare($sql)){
    $stmt->bind_param('sii', $token, $from, $to);
    $stmt->execute();
    $stmt->bind_result($a);
    $stmt->fetch();
    echo $a;
} else {
    echo "not good";
}

当我删除以前的sql查询时,一切正常。

0 个答案:

没有答案