Skip to content
Advertisement

store_result() and get_result() in mysql returns false

I made a code a few days ago including get_result() to receave the results from my database. Today I wantet to add to it and fix some errors. So I tried to use num_rows to see if anything were returned. But for this I had to use store_result(). And when I do this get_result() just returns a boolean of false. When I comment out store_result() everything works as it should. I know that the >= will mess it up. But I put the = there for debugging(to comment out the store_result() and see what happend). So that is not the problem

$sql = $this->connect();
$a = $sql->prepare("SELECT `name`, `title`, `comment`, `date`  FROM `comment` WHERE `post`=?");
$a->bind_param("s", $id);
$a->execute();
$a->store_result();
if ($a->num_rows >= 0) {
    $res = $a->get_result();
    var_dump($res);

    while ($row = $res->fetch_assoc()) {
        $results[] = $row;
    }
    return $results;
} else {
    return false;
}

Advertisement

Answer

Use get_result() instead of store_result(), and then use the result object’s num_rows:

$a->execute();
$res = $a->get_result();
if ($res->num_rows > 0) {
    while ($row = $res->fetch_assoc()) {
        $results[] = $row;
    }
    return $results;
} else {
    return false;
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement