PHP SQLite3 - 绑定值

时间:2013-10-18 19:24:15

标签: php sqlite

当我使用sqliteviewer并运行时:

SELECT * FROM areas WHERE x1 <= 1 AND x2 >= 1 AND z1 <= 1 AND z2 >= 1

我得到了正确的结果,但是使用这个PHP代码我得不到结果:

$x = 1;
$z = 1;
$sql = $this->database->prepare("SELECT * FROM areas WHERE x1 <= :x AND x2 >= :x AND z1 <= :z AND z2 >= :z");
$sql->bindValue(':x', $x, SQLITE3_INTEGER);
$sql->bindValue(':z', $z, SQLITE3_INTEGER);
$result = $sql->execute();

有人知道这段代码有什么问题吗?

修改 它适用于查询:

$result = $this->database->query("SELECT * FROM areas WHERE x1 <= ".$x." AND x2 >= ".$x." AND z1 <= ".$z." AND z2 >= ".$z);

1 个答案:

答案 0 :(得分:2)

与查询方法不同,您必须使用$result->fetchArray()来实际获取结果。 $sql->execute()只返回查询是否成功的布尔值。

所以代码应如下所示:

$x = 1;
$z = 1;
$sql = $this->database->prepare("SELECT * FROM areas WHERE x1 <= :x AND x2 >= :x AND z1 <= :z AND z2 >= :z");
$sql->bindValue(':x', $x, SQLITE3_INTEGER);
$sql->bindValue(':z', $z, SQLITE3_INTEGER);
$boolean = $sql->execute();
$result = $sql->fetchArray();//This line is important.