1

I'm trying to fetch all rows for parentId for my form. However my below code isn't able to fetches just 1 record, into my array:

public function getChildByParent($parentId)
{
    $stmt = $this->conn->prepare("SELECT childId, nick, relation FROM childId WHERE parentId = ?");
    $stmt->bind_param("s", $parentId);
    $stmt->execute();
    $stmt->bind_result($childId, $nick, $relation);
    $stmt->fetch();
    $user = array();
    $user['childId'] = $childId;
    $user['nick'] = $nick;
    $user['relation'] = $relation;
    return $user;
}

I understand that I need to tweek around $stmt->fetch() and $user = array() to fetch_all. Can you help me work around this code?

Appreciate your efforts.

1 Answer 1

1

Using $stmt->get_result() to setup $result->fetch_all() to get all records in one call.

Try:

public function getChildByParent($parentId)
{
    $stmt = $this->conn->prepare("SELECT childId, nick, relation FROM childId WHERE parentId = ?");
    $stmt->bind_param("i", $parentId);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_all(MYSQLI_ASSOC);
    $stmt->close();
    return $user;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.