PHP SQLite3 - 绑定值 [英] PHP SQLite3 - Bind value
本文介绍了PHP SQLite3 - 绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用 sqliteviewer 并运行时:
When I use a sqliteviewer and run:
SELECT * FROM areas WHERE x1 <= 1 AND x2 >= 1 AND z1 <= 1 AND z2 >= 1
我得到了正确的结果,但是使用这个 php 代码我没有得到结果:
I get the right result, but with this php code I don't get a result:
$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();
有人知道这段代码有什么问题吗?
Does anybody know what's wrong with this code?
它适用于查询:
$result = $this->database->query("SELECT * FROM areas WHERE x1 <= ".$x." AND x2 >= ".$x." AND z1 <= ".$z." AND z2 >= ".$z);
推荐答案
与查询方法不同,您必须使用 $result->fetchArray()
才能实际获取结果.$sql->execute()
只会返回查询是否成功的布尔值.
Unlike the query method, you have to use $result->fetchArray()
to actually get the result. $sql->execute()
will only return a boolean value of whether the query has succeeded or not.
所以代码应该是这样的:
So the code should look like this:
$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.
这篇关于PHP SQLite3 - 绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文