1

当我使用 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);
4

1 回答 1

1

与查询方法不同,您必须使用$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.
于 2013-10-18T23:14:49.880 回答