函数bind_param()在布尔值上

时间:2016-07-18 09:20:51

标签: php mysqli

我有这个代码,一个简单的检索mysql数据,但我在提交时收到此错误。

Fatal error: Call to a member function bind_param() on boolean

我知道有很多解决方案,但我似乎无法找到问题出在哪里

这是我的代码: -

$mysqli = new mysqli("localhost", "poke", "password", "pokedb");

    if (isset($_POST['poke_name'])) {

    $query = "SELECT name,lat,long,pic,description FROM creature WHERE name LIKE ?";
    $param = "%{$_POST['poke_name']}%";

    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("s", $param);
    $stmt->execute();
    $stmt->bind_result($name, $lat, $long, $pic, $description);

        /* fetch values */
        while ($pokeinfo = $stmt->fetch_assoc()) {
            $poke_info[] = $pokeinfo;
        }

        /* close statement */
       $mysqli->close();

    }

2 个答案:

答案 0 :(得分:0)

准备工作失败了。测试prepare返回的状态,然后回显$mysqli->error

$mysqli = new mysqli("localhost", "poke", "password", "pokedb");
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (isset($_POST['poke_name'])) {

    $query = "SELECT name,lat,long,pic,description FROM creature WHERE name LIKE ?";
    $param = "%{$_POST['poke_name']}%";


    $stmt = $mysqli->prepare($query);
    if ( $stmt === false ) {
        echo $mysqli->error;
        exit;
    }

答案 1 :(得分:0)

您的查询可以通过以下方式修改:

$query =  "SELECT name,lat,long,pic,description FROM creature WHERE name LIKE '%?%'";
$param =  $_POST['poke_name'];

您应该调试代码。试试这个:

$stmt = $mysqli->prepare($query) or die($mysqli->error);
var_dump($stmt); // Should return true if query executes

在phpmyadmin中运行:

SELECT name,lat,long,pic,description FROM creature WHERE name LIKE '%a%';

如果成功运行,那么准备好的PHP代码就会出错。