0

I am working on fetching data from a table in PHP with the following code. How can I copy all rows directly into an array? I tried fetch_array() instead of fetch() but it was also not successful.

$stmt = $this->db->prepare($sql); 
$stmt->execute();
$stmt->bind_result($aID, $sID);

while ($stmt -> fetch()) { 
    echo $aID . " ". $sID . "<br>";
}           
0

3 Answers 3

1

If using PDO

the Fetch_column is what you are looking for

$stmt = $this->db->prepare($sql); 
        $stmt->execute();
        $stmt->bind_result($aID, $sID);
    $Arrays = $stmt ->fetchAll(PDO::FETCH_COLUMN);
Sign up to request clarification or add additional context in comments.

4 Comments

I tried like this but not echoing. ` $stmt = $this->db->prepare($sql); $stmt->execute(); $stmt->bind_result($aID, $sID); while ($row = $stmt -> fetchAll(PDO::FETCH_COLUMN)){ echo $row[0]; echo $row[1]; }`
First things first; are you using mysqli or PDO? and try my structure and print_r($Arrays); Does this return an array that you are looking for?
Oh..sorry .. I am using mysqli.
This should be mentioned in your question then; Dis-regard this post, it's for PDO
0

How can I copy all rows directly into array?

3 Comments

Will that be like this? ` $stmt = $this->db->prepare($sql); $stmt->execute(); $stmt->bind_result($aID, $sID); $allrows = $stmt->mysqli_fetch_all(); echo $allrows ->rowcount(); `
Assuming you are using MySQLi OOP, the method is just fetch_all(). Reference the links I provided to the PHP Docs. They are your friend.
I changed to this now but still not working. Any idea? While ($row = $stmt -> fetch_all()){ echo $row[0]; echo $row[1];}
0
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);

1 Comment

This answer would be improved with an explanation of what the code is doing/how it would solve the problem. It's currently being identified as a low quality post due to length and content

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.