Fetch Array from class with PDO

John Siniger

Hello I would like to see if someone can help me with the following. I am selecting some results from database with PDO, which SQL query is inside a class file inside /class/functions.php. The problem is that I can't make it display the results in php file inside a form.

Here is the SQL query which I am using inside class/functions.php:

class Users {    
public function selecttema() {
        try{
                $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
                $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                $sql = "SELECT * FROM topics";

                $stmt = $con->prepare( $sql );

                $stmt->setFetchMode(PDO::FETCH_ASSOC);
                $stmt->execute();               

             }catch (PDOException $e) {
                 echo $e->getMessage();
                 }

    }}

After that on the external file I am trying to display the information from this query with the following code:

<?php 
            $select = new Users;
            $select->selecttema();
            while( $row = $stmt->fetch()){ 
            echo '<option value="' . $row['id'] . '">' . $row['topic'] . '</option>';
            } ?>

I am getting the following errors:

Notice: Undefined variable: stmt in E:\xampp\htdocs\doccms\add_doc.php on line 83

Fatal error: Call to a member function fetch() on null in E:\xampp\htdocs\doccms\add_doc.php on line 83

Any help will be welcome. thanks!

Barmar

The variable $stmt is local to the function. You need to return the value:

public function selecttema() {
    try{
        $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); 
        $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sql = "SELECT * FROM topics";

        $stmt = $con->prepare( $sql );

        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->execute();               
        return $stmt;
    }catch (PDOException $e) {
        echo $e->getMessage();
        return false;
    }

}

Then use that in the caller:

$statement = $select->selecttema();
while ($row = $statement->fetch()) {
    echo '<option value="' . $row['id'] . '">' . $row['topic'] . '</option>';
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP PDO fetch data from Array

From Dev

PDO fetch array as row

From Dev

PDO fetch one column from table into 1-dimensional array

From Dev

Read array from PDO using FETCH_KEY_PAIR

From Dev

PDO fetch one column from table into 1-dimensional array

From Dev

PDO fetch single class not working

From Dev

PDO fetch single class not working

From Dev

Opposite To PDO::FETCH_CLASS

From Dev

How to store single value or entire array from fetch(PDO::FETCH_ASSOC) in a variable?

From Dev

Is there an INSERT or UPDATE equivalent to PDO::FETCH_CLASS?

From Dev

How to fetch the first row as object of class? (PDO)

From Dev

Casting Issue in PHP PDO::FETCH_CLASS

From Dev

PDO equivalent of mysql_fetch_array

From Dev

PDO Fetch data returns array of string

From Dev

PDO Fetch Assoc returns the word "Array"

From Dev

translation mysql_fetch_array to PDO::FETCH_NUM

From Dev

translation mysql_fetch_array to PDO::FETCH_NUM

From Dev

Fetch values from one column of a table with PDO

From Dev

PDO::FETCH_CLASS without passing class name as a parameter

From Dev

Having PDO::fetch(PDO::FETCH_CLASS) return a null-valued instance of the object instead of a bool(false)

From Dev

fetch value from an array

From Dev

Can PDO::FETCH_CLASS mode be used for one row (not fetchAll)?

From Dev

PDO FetchAll Fetch_Class not returning all rows/objects

From Dev

Using PDO::FETCH_CLASS to bind table fields to object properties

From Dev

PDO FETCH_CLASS when fields and columns have different names

From Dev

What is the exact equivalent of mysqli_fetch_array function in PDO?

From Dev

Using array_map with fetchAll(PDO::FETCH_ASSOC)

From Dev

fetch value from a given array

From Dev

Need to fetch the numbers from an array

Related Related

  1. 1

    PHP PDO fetch data from Array

  2. 2

    PDO fetch array as row

  3. 3

    PDO fetch one column from table into 1-dimensional array

  4. 4

    Read array from PDO using FETCH_KEY_PAIR

  5. 5

    PDO fetch one column from table into 1-dimensional array

  6. 6

    PDO fetch single class not working

  7. 7

    PDO fetch single class not working

  8. 8

    Opposite To PDO::FETCH_CLASS

  9. 9

    How to store single value or entire array from fetch(PDO::FETCH_ASSOC) in a variable?

  10. 10

    Is there an INSERT or UPDATE equivalent to PDO::FETCH_CLASS?

  11. 11

    How to fetch the first row as object of class? (PDO)

  12. 12

    Casting Issue in PHP PDO::FETCH_CLASS

  13. 13

    PDO equivalent of mysql_fetch_array

  14. 14

    PDO Fetch data returns array of string

  15. 15

    PDO Fetch Assoc returns the word "Array"

  16. 16

    translation mysql_fetch_array to PDO::FETCH_NUM

  17. 17

    translation mysql_fetch_array to PDO::FETCH_NUM

  18. 18

    Fetch values from one column of a table with PDO

  19. 19

    PDO::FETCH_CLASS without passing class name as a parameter

  20. 20

    Having PDO::fetch(PDO::FETCH_CLASS) return a null-valued instance of the object instead of a bool(false)

  21. 21

    fetch value from an array

  22. 22

    Can PDO::FETCH_CLASS mode be used for one row (not fetchAll)?

  23. 23

    PDO FetchAll Fetch_Class not returning all rows/objects

  24. 24

    Using PDO::FETCH_CLASS to bind table fields to object properties

  25. 25

    PDO FETCH_CLASS when fields and columns have different names

  26. 26

    What is the exact equivalent of mysqli_fetch_array function in PDO?

  27. 27

    Using array_map with fetchAll(PDO::FETCH_ASSOC)

  28. 28

    fetch value from a given array

  29. 29

    Need to fetch the numbers from an array

HotTag

Archive